[英]heatmap hashtag and location in python pandas dataframe
I have Pandas Dataframe as below我有 Pandas Dataframe 如下
newdf[['name_left','text']] newdf[['name_left','text']]
from each text column I would like to extract every hashtag and create heatmap with name_left on X axis and extracted hashtag on Y axis从每个文本列中,我想提取每个主题标签并在 X 轴上创建带有 name_left 的热图,并在 Y 轴上提取主题标签
I can perform count of each hashtag using code below我可以使用下面的代码对每个主题标签进行计数
newdf.text.str.extractall(r'(#\w+)').reset_index(level=0).drop_duplicates()[0].value_counts() newdf.text.str.extractall(r'(#\w+)').reset_index(level=0).drop_duplicates()[0].value_counts()
unfortunately im struggling to add name_left and later create heatmap to see correlations不幸的是,我正在努力添加 name_left 并稍后创建热图以查看相关性
I think what you want is this我想你想要的是这个
import pandas as pd
df = pd.DataFrame({'name_left': ['Canada', 'Peru'],
'text': ['asdf #broccoli sadfsd #milk', 'sdfsd #king bbas #toast']})
df = df.groupby(['name_left']).apply(lambda x: x.text.str.extractall(r'(#\w+)').reset_index(level=0).drop_duplicates()[0].value_counts())
print(df)
Canada #milk 1
#broccoli 1
Peru #king 1
#toast 1
Name: 0, dtype: int64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.