[英]Efficiently count occurrences of a value with groupby and within a date range
[英]count occurrences of specific string value after using groupby
我有一个很大的dataframe
,我想按A列分组并计算c
列中出现的字符串。
animal weight color
dog 10 white
dog 11 white
cat 18 white
cat 15 black
bird 16 white
bird 11 black
bird 10 white
df=pd.read_csv('test.csv')
make=df.groupby('animal')['color'].str.contains('white').count()
预期的输出为Dog=2,cat=1,bird=2
所谓的value_counts
df.groupby('animal').color.value_counts().loc[:,'white']
animal
bird 2
cat 1
dog 2
Name: color, dtype: int64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.