[英]Pandas: How to sum second level columns in hierarchical dataframe
I have a DataFrame that looks as follows:我有一个 DataFrame,如下所示:
import pandas as pd
df = pd.DataFrame(data=[[1,2,4,5,1,2], [7,8,10,11,7,8], [13,14,16,17,4,5]], index=pd.date_range('2004-01-01', '2004-01-03'))
df.columns = pd.MultiIndex.from_product([['x', 'y', 'z'], list('ab')])
df
x y z
a b a b a b
2004-01-01 1 2 4 5 1 2
2004-01-02 7 8 10 11 7 8
2004-01-03 13 14 16 17 4 5
I would like to sum the second level columns (a+b) for each first level column and have the first level columns as column names我想对每个第一级列的第二级列 (a+b) 求和,并将第一级列作为列名
You can specify level
and axis
parameter in groupby to aggregate by level 0 of the column index ( axis=1
):您可以在 groupby 中指定
level
和axis
参数,以按列索引 ( axis=1
) 的级别 0 进行聚合:
df.groupby(level=0, axis=1).sum()
x y z
2004-01-01 3 9 3
2004-01-02 15 21 15
2004-01-03 27 33 9
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.