简体   繁体   English

header 中的展平 MultiIndex 交换列名

[英]Flattening MultiIndex in header swaps the column names

I have the following data frame with multiple headers:我有以下带有多个标题的数据框:

    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

When I pass from multiple headers to a single header, at some point the column names are swapped and I don't know how to fix it.当我从多个标题传递到单个 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

This gives me swapped column names:这给了我交换的列名:

    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

How can I fix it?我该如何解决?

Update:更新:

{('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}}

Bruteforce approach蛮力方法

>>> 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

Try with set_index + droplevel + reset_index :尝试使用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

Also to fix your implementation, don't insert into the list and misalign the DataFrame just do:另外要修复您的实现,请不要插入列表并错位 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM