[英]Word Count Distribution Pandas Dataframe
需要从 dataframe 中进行字数分布计数。 有谁知道如何解决?
原始数据:
word
apple pear
pear
best apple pear
所需的 output:
word count
apple 2
pear 3
best 1
运行此代码:
rawData = pd.concat([rawData.groupby(rawData.word.str.split().str[0]).sum(),rawData.groupby(rawData.word.str.split().str[-1]).sum()]).reset_index()
收到此错误:
ValueError: cannot insert keyword, already exists
使用str.split
然后explode
每个列表分解为一列,最后使用value_counts
计算每个单词的出现次数:
out = df['word'].str.split().explode().value_counts()
print(out)
# Output:
pear 3
apple 2
best 1
Name: word, dtype: int64
一步步:
>>> df['word'].str.split()
0 [apple, pear]
1 [pear]
2 [best, apple, pear]
Name: word, dtype: object
>>> df['word'].str.split().explode()
0 apple
0 pear
1 pear
2 best
2 apple
2 pear
Name: word, dtype: object
>>> df['word'].str.split().explode().value_counts()
pear 3
apple 2
best 1
Name: word, dtype: int64
更新
要准确获得您的预期结果:
>>> df['word'].str.split().explode().value_counts(sort=False) \
.rename('count').rename_axis('word').reset_index()
word count
0 apple 2
1 pear 3
2 best 1
更新 2
按国家/地区获取值计数:
data = {'country': [' US', ' US', ' US', ' UK', ' UK', ' UK', ' UK'],
'word': ['best pear', 'apple', 'apple pear',
'apple', 'apple', 'pear', 'apple pear ']}
df = pd.DataFrame(data)
out = df.assign(word=df['word'].str.split()) \
.explode('word').value_counts() \
.rename('count').reset_index()
print(out)
# Output:
country word count
0 UK apple 3
1 UK pear 2
2 US apple 2
3 US pear 2
4 US best 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.