![](/img/trans.png)
[英]Merge two column header and give a new name in MultiIndex Dataframe python/Add column above column names
[英]Flattening MultiIndex in header swaps the column names
我有以下带有多个标题的数据框:
Datetime Value
id a b c d e
0 2017-01-01 00:00:00 0.774016 1.588788 270.06055 268.9109 93060.31
1 2017-01-01 00:10:00 0.774016 1.588788 270.06055 268.9109 93060.31
2 2017-01-01 00:20:00 0.774016 1.588788 270.06055 268.9109 93060.31
3 2017-01-01 00:30:00 0.774016 1.588788 270.06055 268.9109 93060.31
4 2017-01-01 00:40:00 0.774016 1.588788 270.06055 268.9109 93060.31
当我从多个标题传递到单个 header 时,有时列名被交换,我不知道如何修复它。
cols = ["a","b","c","d","e"]
df.columns = [col[1] if col[0] == '' else col[0] for col in df.columns]
cols.insert(0,"Datetime")
df.columns = cols
这给了我交换的列名:
Datetime a b d e c
0 2017-01-01 00:00:00 0.774016 1.588788 270.06055 268.9109 93060.31
1 2017-01-01 00:10:00 0.774016 1.588788 270.06055 268.9109 93060.31
2 2017-01-01 00:20:00 0.774016 1.588788 270.06055 268.9109 93060.31
3 2017-01-01 00:30:00 0.774016 1.588788 270.06055 268.9109 93060.31
4 2017-01-01 00:40:00 0.774016 1.588788 270.06055 268.9109 93060.31
我该如何解决?
更新:
{('Datetime', ''): {0: Timestamp('2017-01-01 00:00:00'),
1: Timestamp('2017-01-01 00:10:00'),
2: Timestamp('2017-01-01 00:20:00'),
3: Timestamp('2017-01-01 00:30:00'),
4: Timestamp('2017-01-01 00:40:00')},
('Value', 'a'): {0: 0.774016,
1: 0.774016,
2: 0.774016,
3: 0.774016,
4: 0.774016},
('Value', 'b'): {0: 1.588788,
1: 1.588788,
2: 1.588788,
3: 1.588788,
4: 1.588788},
('Value', 'c'): {0: 270.06055,
1: 270.06055,
2: 270.06055,
3: 270.06055,
4: 270.06055},
('Value', 'd'): {0: 268.9109,
1: 268.9109,
2: 268.9109,
3: 268.9109,
4: 268.9109},
('Value', 'e'): {0: 93060.31,
1: 93060.31,
2: 93060.31,
3: 93060.31,
4: 93060.31}}
蛮力方法
>>> pd.concat([df[['Datetime']].droplevel(1, axis=1), df["Value"]], axis=1)
Datetime a b c d e
id
0 2017-01-01 00:00:00 0.774016 1.588788 270.06055 268.9109 93060.31
1 2017-01-01 00:10:00 0.774016 1.588788 270.06055 268.9109 93060.31
2 2017-01-01 00:20:00 0.774016 1.588788 270.06055 268.9109 93060.31
3 2017-01-01 00:30:00 0.774016 1.588788 270.06055 268.9109 93060.31
4 2017-01-01 00:40:00 0.774016 1.588788 270.06055 268.9109 93060.31
尝试使用set_index
+ droplevel
+ reset_index
:
df.set_index('Datetime', append=True).droplevel(0, 1).reset_index('Datetime')
Datetime a b c d e
id
0 2017-01-01 00:00:00 0.774016 1.588788 270.06055 268.9109 93060.31
1 2017-01-01 00:10:00 0.774016 1.588788 270.06055 268.9109 93060.31
2 2017-01-01 00:20:00 0.774016 1.588788 270.06055 268.9109 93060.31
3 2017-01-01 00:30:00 0.774016 1.588788 270.06055 268.9109 93060.31
4 2017-01-01 00:40:00 0.774016 1.588788 270.06055 268.9109 93060.31
另外要修复您的实现,请不要插入列表并错位 DataFrame 只需执行以下操作:
df.columns = [col[1] if col[1] else col[0] for col in df.columns]
Datetime a b c d e
id
0 2017-01-01 00:00:00 0.774016 1.588788 270.06055 268.9109 93060.31
1 2017-01-01 00:10:00 0.774016 1.588788 270.06055 268.9109 93060.31
2 2017-01-01 00:20:00 0.774016 1.588788 270.06055 268.9109 93060.31
3 2017-01-01 00:30:00 0.774016 1.588788 270.06055 268.9109 93060.31
4 2017-01-01 00:40:00 0.774016 1.588788 270.06055 268.9109 93060.31
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.