如何在数据框Python中获取具有特定值的最常见单词

时间:2019-05-17 21:01:52

标签: python dataframe

我有一个得分为0和1并有相应评论的数据框,我想在评论中找到0分和1分最常见的单词。我尝试了这个,但是它给出了所有单词的数量:

count = defaultdict(int)
l = df['Summary']
for number in l:
    count[number] += 1

print(count)

如何在所有得分为1和0的行中找到最常见的值?

3 个答案:

答案 0 :(得分:0)

尝试使用频率字典。如果您的列可以被视为列表列表:

data = [[0, "text samle 1"], [0, "text sample 2"], [1, "text sample 3"]]

...那么您可以:

fd0 = dict()
fd1 = dict()
for list_item in data:
    associated_value = list_item[0]

    #note the split(' ') splits the string into a list of words
    for word in list_item[1].split(' '):
        if associated_value == 0:
            fd0[word] = 1 if word not in fd0 else fd0[word] + 1
        elif associated_value == 1:
            fd1[word] = 1 if word not in fd1 else fd1[word] + 1

在循环结束时,您的fd0应该具有标签0的频率,而fd1应该具有标签1的频率。

答案 1 :(得分:0)

假设您的数据如下所示

            review  score
0       bad review      0
1      good review      1
2  very bad review      0
3   movie was good      1

您可以做类似的事情

words = pd.concat([pd.Series(row['score'], row['review'].split(' '))              
    for _, row in df.iterrows()]).reset_index()
words.columns = ['word', 'score']
print(words.groupby(['score', 'word']).size())

为您提供

score  word
0      bad       2
       review    2
       very      1
1      good      2
       movie     1
       review    1
       was       1
dtype: int64

答案 2 :(得分:0)

most_common_0 = ''
most_common_1 = ''

for text, score in zip(df['Summary'], df['Score']):
    if score == 1:
        most_common_1 += ' ' + text
    else:
        most_common_0 += ' ' + text

from collections import Counter
c = Counter(most_common_1.split())
print(c.most_common(2)) # change this 2 to the number you want to analyze

输出

[('good', 2), ('and', 1)]
相关问题