Python 使用groupby后统计特定字符串值的出现次数
我有一个很大的数据框,我想在其中按列a分组,并计算列c中字符串的出现次数 预期输出为Dog=2、cat=1、bird=2,即所谓的值计数Python 使用groupby后统计特定字符串值的出现次数,python,pandas,Python,Pandas,我有一个很大的数据框,我想在其中按列a分组,并计算列c中字符串的出现次数 预期输出为Dog=2、cat=1、bird=2,即所谓的值计数 animal weight color dog 10 white dog 11 white cat 18 white cat 15 black bird 16 white bird 11 black bird 10
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()
df.groupby('animal').color.value_counts().loc[:,'white']
animal
bird 2
cat 1
dog 2
Name: color, dtype: int64