为了账号安全,请及时绑定邮箱和手机立即绑定

计算数据框中的标签频率

计算数据框中的标签频率

SMILET 2023-04-25 16:41:28
我正在尝试计算数据框“文本”列中主题标签词的频率。index        text1            ello ello ello ello #hello #ello2            red green blue black #colours3            Season greetings #hello #goodbye 4            morning #goodMorning #hello5            my favourite animal #dogword_freq = df.text.str.split(expand=True).stack().value_counts()上面的代码将对文本列中的所有字符串执行频率计数,但我只是返回标签频率。例如,在我上面的数据框上运行代码后,它应该返回#hello        3#goodbye      1#goodMorning  1#ello         1#colours      1#dog          1有没有一种方法可以稍微重新调整我的 word_freq 代码,以便它只计算标签词并按照我上面的方式返回它们?提前致谢。
查看完整描述

3 回答

?
慕妹3146593

TA贡献1820条经验 获得超9个赞

Series.str.findall在列上使用text查找所有主题标签词,然后使用Series.explodeSeries.value_counts

counts = df['text'].str.findall(r'(#\w+)').explode().value_counts()

Series.str.split使用+的另一个想法DataFrame.stack

s = df['text'].str.split(expand=True).stack()
counts = s[lambda x: x.str.startswith('#')].value_counts()

结果:

print(counts)

#hello          3

#dog            1

#colours        1

#ello           1

#goodMorning    1

#goodbye        1

Name: text, dtype: int64


查看完整回答
反对 回复 2023-04-25
?
aluckdog

TA贡献1847条经验 获得超7个赞

使用它的一种方法是从结果中str.extractall删除。#那么value_counts


s = df['text'].str.extractall('(?<=#)(\w*)')[0].value_counts()

print(s)

hello          3

colours        1

goodbye        1

ello           1

goodMorning    1

dog            1

Name: 0, dtype: int64


查看完整回答
反对 回复 2023-04-25
?
守候你守候我

TA贡献1802条经验 获得超10个赞

一个稍微详细的解决方案,但这可以解决问题。


dictionary_count=data_100.TicketDescription.str.split(expand=True).stack().value_counts().to_dict()


dictionary_count={'accessgtgtjust': 1,

'sent': 1,

'investigate': 1,

'edit': 1,

'#prd': 1,

'getting': 1}


ert=[i for i in list(dictionary_count.keys()) if '#' in i]


ert

Out[238]: ['#prd']


unwanted = set(dictionary_count.keys()) - set(ert)


for unwanted_key in unwanted: 

   del dictionary_count[unwanted_key]


dictionary_count

Out[241]: {'#prd': 1}


查看完整回答
反对 回复 2023-04-25
  • 3 回答
  • 0 关注
  • 89 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信