python - Python dataframe group by index level rather than index names
问题描述
I'm having a multi-indexed dataframe. I understand that groupby(level=0) means groupby the first index, then how can I group the data by the first two indices using this method?
I tried groupby(level=0 and 1), and it did not work.
解决方案
Use list like:
df.groupby(level=[0,1])
Also if want use aggregate function like sum
, mean
, max
, min
:
df.groupby(level=[0,1]).sum()
is possible simplify to:
df.sum(level=[0,1])
推荐阅读
- windows - 从另一个区域复制 EC2 实例会带来错误的路由表
- formal-verification - TLA+ 中的咖啡罐问题:无法表达任务
- github-pages - 雨果 github 动作 404
- .htaccess - 如何使用 htaccess 从 URL 中查找和替换特定单词
- javascript - 具有前瞻性的正则表达式不像我期望的那样工作
- python - Python Watermarking - 防止图像大小和质量降低
- apache-kafka - Kafka Connect MQTT:是否可以在 MQTT 和 Kafka 主题之间进行 1:1 映射?
- java - 控制器无法映射对象并带来列表结果
- sql - 如何从日期检查日期名称。但是从程序来看,我的日期错误,所以请帮助我
- node.js - nodejs中的参数问题