在列中,计算逗号分隔的句子中的单词 [英] 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 x in df.Value])).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
on ',',stack
将列转换为行索引并使用 value_counts
.
Using the str
accessor for pandas then split
on ',', stack
the columns into the row index and use value_counts
.
这篇关于在列中,计算逗号分隔的句子中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文