如何从DataFrame中删除MultiIndex?

时间:2019-10-02 21:31:43

标签: python pandas multi-index

我尝试使用 df.columns 从数据框中提取列,然后得到:

MultiIndex([(     'time', ''),
        ('numbers', 11),
        ('numbers', 12),
        ('numbers', 13),
        ('numbers', 14)],
       names=[None, 'letters'])

我以前从未使用过MultiIndex,所以现在我很困惑如何现在将“时间”作为列而不是索引来获取,因此我可以使用此DataFrame:

df = 

                      time     numbers                           
letters                                a       b       c       d
0                     22:45:00      1016.0  1059.0  1042.0  1007.0
1                     23:00:00      1006.0  10507.0  1040.0  1084.0
2                     23:15:00      1084.0  1058.0  1047.0  1495.0
3                     23:30:00      1095.0  1498.0  1480.0  1048.0
4                     23:45:00      1098.0  1002.0  1044.0  1084.0
5                     00:00:00      1044.0  1517.0  1084.0  1051.0

(最好删除MultiIndex) 因此它类似于:

df = 

                      time               a       b       c       d
0                     22:45:00      1016.0  1059.0  1042.0  1007.0
1                     23:00:00      1006.0  1007.0  1040.0  1084.0
2                     23:15:00      1084.0  1058.0  1047.0  1495.0
3                     23:30:00      1095.0  1498.0  1480.0  1048.0
4                     23:45:00      1098.0  1002.0  1044.0  1084.0
5                     00:00:00      1044.0  1517.0  1084.0  1051.0

我尝试使用droplevel但得到了

  

无法从具有1个级别的索引中删除1个级别:必须至少保留一个级别。

这是因为索引位于列而不是行中吗?

1 个答案:

答案 0 :(得分:1)

IIUC,你有这个:

dd = {('time', ''): {0: '22:45:00',
  1: '23:00:00',
  2: '23:15:00',
  3: '23:30:00',
  4: '23:45:00',
  5: '00:00:00'},
 ('numbers', 'a'): {0: 1016.0,
  1: 1006.0,
  2: 1084.0,
  3: 1095.0,
  4: 1098.0,
  5: 1044.0},
 ('numbers', 'b'): {0: 1059.0,
  1: 10507.0,
  2: 1058.0,
  3: 1498.0,
  4: 1002.0,
  5: 1517.0},
 ('numbers', 'c'): {0: 1042.0,
  1: 1040.0,
  2: 1047.0,
  3: 1480.0,
  4: 1044.0,
  5: 1084.0},
 ('numbers', 'd'): {0: 1007.0,
  1: 1084.0,
  2: 1495.0,
  3: 1048.0,
  4: 1084.0,
  5: 1051.0}}

 df1 = pd.DataFrame(dd).rename_axis([None,'letters'], axis=1)
 df1 

输入数据框:

             time numbers                         
letters                 a        b       c       d
0        22:45:00  1016.0   1059.0  1042.0  1007.0
1        23:00:00  1006.0  10507.0  1040.0  1084.0
2        23:15:00  1084.0   1058.0  1047.0  1495.0
3        23:30:00  1095.0   1498.0  1480.0  1048.0
4        23:45:00  1098.0   1002.0  1044.0  1084.0
5        00:00:00  1044.0   1517.0  1084.0  1051.0

然后

df2 = df1.set_index('time')

df2.columns = df2.columns.droplevel(0)

df2.reset_index()

输出:

letters      time       a        b       c       d
0        22:45:00  1016.0   1059.0  1042.0  1007.0
1        23:00:00  1006.0  10507.0  1040.0  1084.0
2        23:15:00  1084.0   1058.0  1047.0  1495.0
3        23:30:00  1095.0   1498.0  1480.0  1048.0
4        23:45:00  1098.0   1002.0  1044.0  1084.0
5        00:00:00  1044.0   1517.0  1084.0  1051.0