python - 如何通过计算我的 pandas 数据框中的值来创建一个新系列?
问题描述
我有一个看起来像这样的熊猫数据框:
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
我需要做的是通过计算animal_category中 dog、cat、bird 等的数量来创建一个新系列。我的新系列应该是这样的:
animal_names number of animals
dog 50
cat 20
bird 15
other 30
我怎样才能做到这一点?
解决方案
另一种选择(来自@Quang Hoang 的回答)是使用groupby()
output_df = df.groupby(['animal_category'],as_index=False)['like_count'].count().rename(columns={'like_count':'number of animals'})
推荐阅读
- java - Android平台图片上传代码错误
- xmpp - 响应解码失败,xmpp sasl 身份验证
- javascript - 为什么我的 jquery 输入验证不起作用?
- gatsby - Queryql-playground 可以全局安装在 Gatsby 中吗?
- opengl - GLSL 编译器会删除不必要的变量初始化吗?
- python - 将df写入csv时如何修复错误的数字格式?
- json - Splitting nested arrays as separate entities
- python - 如何重定向到 Django 中新创建的对象?
- java - android data binding error: cannot find symbol
- swift - 如何处理单元格中的多个文本字段并获取文本字段数据