我想计算这些ngram在一个装满文章的数据帧列(df.content)中出现的次数。 我的数据帧是:
我的ngram列表如下:
df_ngrams中的'value'列显示了特定ngram在df.content中出现的次数,但我想计算每一篇文章的次数
我已经试过了:
ngrams_count = ['u s', 'president trump', 'donald trump', 'united states', 'white house', 'new york', 'hillary clinton', 'fox news', 'donald trumps', 'president donald']
count = 0
articleCount = 0
for i in df.content:
articleCount += 1
for j in ngrams_count:
if j in i:
count = i.find(j)
print ("article ", articleCount, "has ", count, " instances of ngram ", j)
counts = 0
试试这个:
样本输出:
不要试图提供不同的方法,也不要利用你的方法,这里是你需要的改变。你需要数一数一克发生的次数,而不是找不到它。find()只提供找到它的索引,如果找不到文本,则返回-1(这不是您想要的):
相关问题 更多 >
编程相关推荐