[英]Pandas value_count where row name
Count Month Fruit
1 Mar Apple
2 Apr Kiwi
4 Jun Orange
8 Dec Kiwi
12 Nov Kiwi
4 Oct Melon
With 同
counts = ou['Fruit'].value_counts().to_frame()
I get 我明白了
Apple: 1
Kiwi: 3
Orange: 1
Melon: 1
I tried something like that 我尝试过类似的东西
counts = ou['Fruit'].where("Kiwi").value_counts()
How is it possible to count only the kiwis? 怎么可能只计算猕猴桃? that the output is only 3 or Kiwis: 3
输出只有3或新西兰:3
I recreate your data - and then give you the number of occurrences of 'Kiwi'. 我重新创建你的数据 - 然后给你'Kiwi'的出现次数。 If you want the total then just change the count() to sum()
如果你想要总数,那么只需将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()
If need length of Kiwi
only sum True
values only: 如果需要长度为
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
Same, but a bit complicated: 相同,但有点复杂:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.