Count   Month   Fruit
1       Mar     Apple
2       Apr     Kiwi
4       Jun     Orange
8       Dec     Kiwi
12      Nov     Kiwi
4       Oct     Melon




counts = ou['Fruit'].value_counts().to_frame()


我懂了

Apple:  1
Kiwi:   3
Orange: 1
Melon:  1


我尝试过这样的事情

counts = ou['Fruit'].where("Kiwi").value_counts()


如何只计算猕猴桃?
只是输出
3或猕猴桃:3

最佳答案

我重新创建您的数据-然后给您“奇异果”的出现次数。如果您想要总数,则只需将count()更改为sum()

import pandas as pd

d={'count':[1,2,4,8,12,4],
'Month':['Mar','Apr','Jun','Dec','Nov','Oct'],
  'Fruit':['Apple','Kiwi','Orange','Kiwi','Kiwi','Melon']}
df=pd.DataFrame(d)
df[df.Fruit=='Kiwi'].count()

关于python - Pandas value_count其中行名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43341638/

10-15 01:01