每个pandas数据帧行的字频率

时间:2018-04-21 22:52:18

标签: python string pandas dataframe

我试图弄清楚每个数据帧行如何获得最频繁的单词 - 让我们说出前10个最常用的单词。我有一些代码可以让我对整个DF有最常用的话,但现在我需要更细化。

package.class

enter image description here

现在我可以在整个df1上获得最常用的词语:

import pandas as pd
import numpy as np
df1 = pd.read_csv('C:/temp/comments.csv',encoding='latin-1',names=['client','comments'])
df1.head(3)

如何在每个df行获取该信息?

1 个答案:

答案 0 :(得分:1)

根据您是否需要数据框,一系列词典或词典列表,有几种不同的方法可以执行此操作。

from collections import Counter

# dataframe of word counts per row
res = df['comments'].str.split().apply(pd.value_counts)

# series of dictionaries of word counts, each series entry covering one row
res = df['comments'].str.split().apply(Counter)

# list of dictionaries of word counts, each list item covering one row
res = [Counter(x) for x in df['comments'].str.split()]
相关问题