Python:如何打破特殊字符';'?

我怎样才能分解;以计算数量Fruit

数据框如下所示:

     ID         Fruit
0    123       <apple>;<orange>;<lemon>;<pear>
1    456       <lemon>;<lemon>;<lemon>;<kiwi>
2    789       <orange>;<apple>;<kiwi>;<apple>

预期结果:

apple:  3
orange: 2
lemon:  4
pear:   1
kiwi:   2

回答

在你的情况下split然后explode+value_counts

out = df.Fruit.str.split(';').explode().value_counts()
Out[163]: 
<lemon>     4
<apple>     3
<kiwi>      2
<orange>    2
<pear>      1
Name: Fruit, dtype: int64

使其与您的输出相匹配 str.strip('<|>')

df.Fruit.str.split(';').explode().str.strip('<|>').value_counts()
Out[164]: 
lemon     4
apple     3
orange    2
kiwi      2
pear      1
Name: Fruit, dtype: int64


以上是Python:如何打破特殊字符';'?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>