How to remove levels from a multi-indexed dataframe?

前端 未结 3 513
温柔的废话
温柔的废话 2020-12-04 10:38

For example, I have:

In [1]: df = pd.DataFrame([8, 9],
                          index=pd.MultiIndex.from_tuples([(1, 1, 1),
                                         


        
3条回答
  •  余生分开走
    2020-12-04 11:01

    You don't need to create a new DataFrame instance! You can modify the index:

    df.index = df.index.droplevel(2)
    df
    
         A
    1 1  8
      3  9
    

    You can also specify negative indices, for selection from the end:

    df.index = df.index.droplevel(-1)
    

提交回复
热议问题