合并两个带有子标题的数据框

Merge two dataframes with subheaders

所以我有我的第一个数据框,其中国家/地区为 headers,感染和死亡值为 subheaders,

df
Dates       Antigua & Barbuda      Australia
          Infected      Dead      Infected   Dead
2020-01-22    0          0            0        0...
2020-01-23    0          0            0        0...
...

然后我有了第二个数据框,

df_indicators
Dates       Location      indicator_1      indicator_2 .....
2020-04-24  Afghanistan      0                  0
2020-04-25  Afghanistan      0                  0
...
2020-04-24  Yemen            0                  0
2020-04-25  Yemen            0                  0

我想合并数据框,使指标列成为国家列的子headers,如 df 中受感染和死亡的子headers。

我要制作的是这样的,

df_merge
Dates        Antigua & Barbuda
        Infected    Dead   indicator_1   indicator_2....
2020-04-24  0         0        0             0...

有太多的指标都被命名为不同的东西,我觉得我不能把它们都命名,所以不确定是否有办法可以轻松做到这一点。

提前感谢您的帮助!

因为有重复先按mean聚合然后按Series.unstack with DataFrame.swaplevel整形:

df2 = df_indicators.groupby(['Dates','Location']).mean().unstack().swaplevel(0,1,axis=1)

DataFrame.pivot_table:

df2 = (df.pivot_table(index='Dates', columns='Location', aggfunc='mean')
         .swaplevel(0,1,axis=1))

最后加入排序 MultiIndex in columns:

df = pd.concat([df, df2], axis=1).sort_index(axis=1)