计算熊猫数据框中特定单词的出现次数

时间:2021-05-03 11:44:19

标签: python pandas

我想计算数据帧的每一行提到单词列表的次数 使用此代码

df["Count"] = (
    df['Token'].str.split()
    .apply(Counter)
    .apply(lambda counts: sum(word in counts for word in words))
)

我使用的是单词列表。

words = ['wooly', 'girl']

但是代码导致每个条目的值都为 0,这是不正确的。

我使用的数据是一个标记化的列表,如下所示:['uno', 'dos', 'one', 'two', 'tres', 'quatro', 'yes', 'wooly', 'bully', 'watch', 'watch', 'come', 'come', 'watch', 'git', 'matty', 'told', 'hattie', 'thing', 'saw', 'two', 'big', 'horns', 'wooly', 'jaw', 'wooly', 'bully', 'wooly', 'bully', 'yes', 'drive', 'wooly', 'bully', 'wooly', 'bully', 'wooly', 'bully', 'hattie', 'told', 'matty', 'lets', 'dont', 'take', 'chance', 'lets', 'lseven', 'come', 'learn', 'dance', 'wooly', 'bully', 'wooly', 'bully', 'wooly', 'bully', 'wooly', 'bully', 'wooly', 'bully', 'watch', 'watch', 'watch', 'watch', 'yeah', 'yeah', 'drive', 'drive', 'drive', 'matty', 'told', 'hattie', 'thats', 'thing', 'get', 'someone', 'really', 'pull', 'wool', 'wooly', 'bully', 'wooly', 'bully', 'wooly', 'bully', 'wooly', 'bully', 'wooly', 'bully', 'watch', 'watch', 'come', 'got', 'got']

这个列表我通过 df['Token'] = df['Token'].apply(str) 变成了一个字符串

4 个答案:

答案 0 :(得分:0)

易于使用 defaultdict 中的 Countercollections

words = ['uno', 'dos', 'one', 'two', 'tres', 'quatro', 'yes', 'wooly', 'bully', 'watch', 'watch', 'come', 'come', 'watch', 'git', 'matty', 'told', 'hattie', 'thing', 'saw', 'two', 'big', 'horns', 'wooly', 'jaw', 'wooly', 'bully', 'wooly', 'bully', 'yes', 'drive', 'wooly', 'bully', 'wooly', 'bully', 'wooly', 'bully', 'hattie', 'told', 'matty', 'lets', 'dont', 'take', 'chance', 'lets', 'lseven', 'come', 'learn', 'dance', 'wooly', 'bully', 'wooly', 'bully', 'wooly', 'bully', 'wooly', 'bully', 'wooly', 'bully', 'watch', 'watch', 'watch', 'watch', 'yeah', 'yeah', 'drive', 'drive', 'drive', 'matty', 'told', 'hattie', 'thats', 'thing', 'get', 'someone', 'really', 'pull', 'wool', 'wooly', 'bully', 'wooly', 'bully', 'wooly', 'bully', 'wooly', 'bully', 'wooly', 'bully', 'watch', 'watch', 'come', 'got', 'got']


from collections import defaultdict
dict_count = defaultdict(int)
for item in words:
    dict_count[item] += 1

或者:

from collections import Counter
counts = Counter(words)

答案 1 :(得分:0)

要计算字符串中子字符串出现的次数,您可以这样做

string.count(substring)

因此,您可以将此函数应用于包含字符串的列:

string_occurrences = df.Token.apply(lambda x: sum([x.count(substring) for substing in ['wooly', 'girl']])

然后你只需要对计数求和

total_occurrences = string_occurrences.sum()

答案 2 :(得分:0)

Counter 的返回是字典。

df["Count"] = (
    df['Token'].str.split()
    .apply(Counter)
    .apply(lambda counts: sum([counts[word] for word in words]))
)

如果 Token 列中的值已经是列表,则无需使用 str.split()

df["Count"] = (
    df['Token']
    .apply(Counter)
    .apply(lambda counts: sum([counts[word] for word in words]))
)

答案 3 :(得分:0)

要计算 pandas 数据框中特定单词的出现次数,您可以使用以下代码。

from collections import Counter
    
def most_common_words(labels, quantity):

    words = [i.split(" ", 1)[0] for i in labels]
    counter = Counter(words).most_common(quantity)
    df = pd.DataFrame(counter, columns=["Word", "Occurrence number"])\
                        .sort_values(by="Occurrence number", ascending=True)
    return df

    
# Identify the most common words
df_most_common_words = most_common_words(words, 20)
print(df_most_common_words)