我是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)]
我想数一下整列中某个特定单词的使用次数。所以我可以预测这是标签中最常用的词。
有人能提出最好的方法吗?我真的很感激你的帮助。
据我所知,您正试图将Counter用于tags_split
中的所有标签
查看Python标准库中的Counter.update()
方法。
import re
from collections import Counter
tags_split = youtube_df.tags.head(3)
counter = Counter() # Initializing a counter variable
for tag in tags_split:
split_strng = re.findall(r"w+",tag)
counter.update(split_strng)
most_common = counter.most_common(3)
print(most_common)
你可以试试这个:
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])