【问题标题】:how to find most common word from the entire column of string in python如何从python中的整个字符串列中找到最常用的单词
【发布时间】:2020-10-29 14:51:55
【问题描述】:

我是 Python 的新手,正在学习它以进行数据分析。

我遇到了一个问题,我的数据集包含列名称标签。这个 Youtube 标签是一个包含各种单词的字符串。我需要找出整个专栏中最常用的词。

数据集名称:youtube_df

列名:标签

tags_split = youtube_df.tags.head(3)
tags_split

import re
from collections import Counter

for t in tags_split:
   #print(t)
   split_strng = re.findall(r"[\w]+",t)
   print(split_strng)
   counter = Counter(split_strng)
   most_common = counter.most_common(3)
   print(most_common)

输出

['Eminem', 'Walk', 'On', 'Water', 'Aftermath', 'Shady', 'Interscope', 'Rap']
[('Eminem', 1), ('Walk', 1), ('On', 1)]
['plush', 'bad', 'unboxing', 'unboxing', 'fan', 'mail', 'idubbbztv', 'idubbbztv2', 'things', 
'best', 'packages', 'plushies', 'chontent', 'chop']
[('unboxing', 2), ('plush', 1), ('bad', 1)]
['racist', 'superman', 'rudy', 'mancuso', 'king', 'bach', 'racist', 'superman', 'love', 'rudy', 
'mancuso', 'poo', 'bear', 'black', 'white', 'official', 'music', 'video', 'iphone', 'x', 'by', 
'pineapple', 'lelepons', 'hannahstocking', 'rudymancuso', 'inanna', 'anwar', 'sarkis', 'shots', 
'shotsstudios', 'alesso', 'anitta', 'brazil', 'Getting', 'My', 'Driver', 's', 'License', 'Lele', 
'Pons']
[('racist', 2), ('superman', 2), ('rudy', 2)]

我想计算整个列中某个特定单词的使用次数。所以我可以预测这是标签中最常用的词。

任何人都可以提出最好的方法吗?我非常感谢任何帮助。

【问题讨论】:

  • 请提供数据框作为代码

标签: python pandas data-analysis data-cleaning


【解决方案1】:

据我了解,您正在尝试对 tags_split 中的所有标签使用计数器

查看更新方法:https://docs.python.org/2/library/collections.html#collections.Counter.update

tags_split = youtube_df.tags.head(3)
tags_split

import re
from collections import Counter

counter = Counter()

for t in tags_split:
   #print(t)
   split_strng = re.findall(r"[\w]+",t)
   counter.update(split_strng)

most_common = counter.most_common(3)
print(most_common)

【讨论】:

    【解决方案2】:

    你可以试试这个:

    m=[]
    for t in tags_split:
       split_strng = re.findall(r"[\w]+",t)
       m.extend(split_strng)
    
    l=Counter(m)
    most_common=max([(i,k) for i,k in l.items()], key=lambda x: x[1])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-14
      • 2020-04-26
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 2011-07-02
      相关资源
      最近更新 更多