I have a pandas dataframe:
import pandas as pd
test = pd.DataFrame({'words':[['foo','bar none','scare','bar','foo'],
['race','bar none','scare'],
['ten','scare','crow bird']]})
I'm trying to get a word/phrase count of all the list elements in the dataframe colunn. My current solution is:
allwords = []
for index, row in test.iterrows():
for word in row['words']:
allwords.append(word)
from collections import Counter
pd.Series(Counter(allwords)).sort_values(ascending=False)
This works, but I was wondering if there was a faster solution. Note: I'm not using ' '.join()
because I don't want the phrases to be split into individual words.
question from:
https://stackoverflow.com/questions/65844350/get-word-frequency-of-pandas-column-containing-lists-of-strings 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…