计算数据帧中特定单词的出现次数

2024-04-24 05:13:53 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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)将此列表转换为字符串


Tags: 数据代码tokendf列表drive单词watch
3条回答

易于使用的defaultdictCounter来自collections

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)

要计算子字符串在字符串中出现的次数,可以执行以下操作

string.count(substring)

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

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

然后你只需要把计数加起来

total_occurrences = string_occurrences.sum()

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]))
)

相关问题 更多 >