[英]How can I create a new series by calculating the values in my pandas data frame?
I have a pandas data frame which looks like this: 我有一个熊猫数据框,看起来像这样:
like_count dislike_count dog_video cat_video bird_video other_animal_video animal_category
1000 500 True False False False dog
5500 300 False True False False cat
2300 100 False False True False bird
1100 200 False False False True other
What I need to do is to create a new series by calculating the number of dog, cat, bird and other in the animal_category . 我需要做的是通过计算animal_category中的狗,猫,鸟和其他动物的数量来创建一个新系列。 My new series should look like this:
我的新系列应如下所示:
animal_names number of animals
dog 50
cat 20
bird 15
other 30
How can I do this? 我怎样才能做到这一点?
另一种选择(来自@Quang Hoang的答案)是使用groupby()
output_df = df.groupby(['animal_category'],as_index=False)['like_count'].count().rename(columns={'like_count':'number of animals'})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.