3 回答

TA貢獻1820條經驗 獲得超9個贊
Series.str.findall
在列上使用text
查找所有主題標簽詞,然后使用Series.explode
+?Series.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

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

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}
添加回答
舉報