在列中,用逗号分隔的句子对单词进行计数 [英] At column, Count word in comma-separated sentence
本文介绍了在列中,用逗号分隔的句子对单词进行计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我的数据框是
Name Value
0 K apple,banana
1 Y banana
2 B orange,banana
3 Q grape,apple
4 C apple,grape
我想在值"列中计算字数,所以当我像这样申请时
I want to count word in 'Value' column so when I applied like
pd.Series(np.concatenate([x.split()for df.Value中的x])).value_counts()
pd.Series(''.join(df.Value).split()).value_counts()
作为输出:
apple,banana : 1
banana : 1
orange,banana : 1
grape,apple : 1
apple,grape : 1
但是
输出我想要的内容
apple : 3
banana : 3
orange : 1
grape : 2
我该怎么做?
感谢您的阅读.
推荐答案
尝试以下方法:
df['Value'].str.split(',', expand=True).stack().value_counts()
输出:
apple 3
banana 3
grape 2
orange 1
dtype: int64
对熊猫使用 str
访问器,然后对','进行 split
,然后将 stack
列插入行索引并使用 value_counts
.
Using the str
accessor for pandas then split
on ',', stack
the columns into the row index and use value_counts
.
这篇关于在列中,用逗号分隔的句子对单词进行计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文