小编v_c*_*r12的帖子

pythonic 方法来计算列表/集合中的单词在数据帧列中出现的次数

给定一个列表/一组标签

labels = {'rectangle', 'square', 'triangle', 'cube'}
Run Code Online (Sandbox Code Playgroud)

和一个数据框 df,

df = pd.DataFrame(['rectangle rectangle in my square cube', 'triangle circle not here', 'nothing here'], columns=['text'])
Run Code Online (Sandbox Code Playgroud)

我想知道标签集中的每个单词在数据框的文本列中出现了多少次,并创建一个新列,其中包含前 X 个(可能是 2 或 3 个)最重复的单词。如果 2 个单词重复次数相同,那么它们可以出现在列表或字符串中

输出:

pd.DataFrame({'text' : ['rectangle rectangle in my square cube', 'triangle circle not here', 'nothing here'], 'best_labels' : [{'rectangle' : 2, 'square' : 1, 'cube' : 1}, {'triangle' : 1, 'circle' : 1}, np.nan]})                                                                                                                          
                                                                                                                      
df['best_labels'] = some_function(df.text) 
Run Code Online (Sandbox Code Playgroud)

python count dataframe pandas find-occurrences

3
推荐指数
1
解决办法
89
查看次数

标签 统计

count ×1

dataframe ×1

find-occurrences ×1

pandas ×1

python ×1