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
答案 0 :(得分:2)
我重新创建你的数据 - 然后给你'Kiwi'的出现次数。如果你想要总数,那么只需将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()
答案 1 :(得分:1)
如果需要Kiwi
的长度仅限True
个值:
print (ou['Fruit'] == 'Kiwi')
0 False
1 True
2 False
3 True
4 True
5 False
Name: Fruit, dtype: bool
print ((ou['Fruit'] == 'Kiwi').sum())
3
同样,但有点复杂:
print (ou['Fruit'].value_counts())
Kiwi 3
Melon 1
Orange 1
Apple 1
Name: Fruit, dtype: int64
counts = ou['Fruit'].value_counts().loc['Kiwi']
print (counts)
3