【发布时间】:2020-05-17 12:26:21
【问题描述】:
我有一个带有两个索引的数据框,如下所示:
Index1 Index2 200701 200702 200703
alphas Fourth Quartile 41.7421 41.1807 39.071
Third Quartile 74.1573 95.0195 90.6572
Second Quartile -34.2001 -42.0068 -21.6236
First Quartile 39.293 37.3475 34.1704
All_Quartiles 37.6624 38.5957 38.0504
betas Fourth Quartile 18.1041 23.0865 33.7109
Third Quartile -51.9743 -93.1191 -87.1772
Second Quartile 121.262 131.556 103.549
First Quartile 26.1859 28.5129 31.8663
All_Quartiles 24.511 23.1601 0.159067
我需要新的索引,像这样:
New_index Index1 Index 2 200701 200702 200703
Sector alphas Fourth Quartile 41.7421 41.1807 39.071
Third Quartile 74.1573 95.0195 90.6572
Second Quartile -34.2001 -42.0068 -21.6236
First Quartile 39.293 37.3475 34.1704
All_Quartiles 37.6624 38.5957 38.0504
betas Fourth Quartile 18.1041 23.0865 33.7109
Third Quartile -51.9743 -93.1191 -87.1772
Second Quartile 121.262 131.556 103.549
First Quartile 26.1859 28.5129 31.8663
All_Quartiles 24.511 23.1601 0.159067
我有许多数据帧多索引属于不同的部门,我需要用一个循环合并每个数据帧。
【问题讨论】:
-
您真的需要为单个 DataFrame 执行此操作,还是仅在组合它们时添加
'New_index'级别就足够了,这似乎是最自然的?如果是后者,您正在寻找pd.concat的keys参数。 -
不,我不能使用
pd.concat和参数keys我需要使用df.append但首先我需要创建新索引
标签: python pandas dataframe concatenation multi-index