Reset a column's MultiIndex levels

后端 未结 3 1171
忘了有多久
忘了有多久 2020-12-14 01:14

Is there a shorter way of dropping a column MultiIndex level (in my case, basic_amt) except transposing it twice?

In [704]: test
Out[704]: 
             


        
相关标签:
3条回答
  • 2020-12-14 01:44

    Zip levels together

    Here is an alternative solution which zips the levels together and joins them with underscore.

    Derived from the above answer, and this was what I wanted to do when I found this answer. Thought I would share even if it does not answer the exact above question.

    ["_".join(pair) for pair in df.columns]
    

    gives

    ['basic_amt_NSW', 'basic_amt_QLD', 'basic_amt_VIC', 'basic_amt_All']
    

    Just set this as a the columns

    df.columns = ["_".join(pair) for pair in df.columns]
    
               basic_amt_NSW  basic_amt_QLD  basic_amt_VIC  basic_amt_All
    Faculty                                                              
    All                    1              1              2              4
    Full Time              0              1              0              1
    Part Time              1              0              2              3
    
    0 讨论(0)
  • 2020-12-14 01:47

    How about simply reassigning df.columns:

    levels = df.columns.levels
    labels = df.columns.labels
    df.columns = levels[1][labels[1]]
    

    For example:

    import pandas as pd
    
    columns = pd.MultiIndex.from_arrays([['basic_amt']*4,
                                         ['NSW','QLD','VIC','All']])
    index = pd.Index(['All', 'Full Time', 'Part Time'], name = 'Faculty')
    df = pd.DataFrame([(1,1,2,4),
                       (0,01,0,1),
                       (1,0,2,3)])
    df.columns = columns
    df.index = index
    

    Before:

    print(df)
    
               basic_amt               
                     NSW  QLD  VIC  All
    Faculty                            
    All                1    1    2    4
    Full Time          0    1    0    1
    Part Time          1    0    2    3
    

    After:

    levels = df.columns.levels
    labels = df.columns.labels
    df.columns = levels[1][labels[1]]
    print(df)
    
               NSW  QLD  VIC  All
    Faculty                      
    All          1    1    2    4
    Full Time    0    1    0    1
    Part Time    1    0    2    3
    
    0 讨论(0)
  • 2020-12-14 01:57

    Another solution is to use MultiIndex.droplevel with rename_axis (new in pandas 0.18.0):

    import pandas as pd
    
    cols = pd.MultiIndex.from_arrays([['basic_amt']*4,
                                         ['NSW','QLD','VIC','All']], 
                                         names = [None, 'Faculty'])
    idx = pd.Index(['All', 'Full Time', 'Part Time'])
    
    df = pd.DataFrame([(1,1,2,4),
                       (0,1,0,1),
                       (1,0,2,3)], index = idx, columns=cols)
                       
    print (df)
              basic_amt            
    Faculty         NSW QLD VIC All
    All               1   1   2   4
    Full Time         0   1   0   1
    Part Time         1   0   2   3
    
    df.columns = df.columns.droplevel(0)
    #pandas 0.18.0 and higher
    df = df.rename_axis(None, axis=1)
    #pandas bellow 0.18.0
    #df.columns.name = None
    
    print (df)
               NSW  QLD  VIC  All
    All          1    1    2    4
    Full Time    0    1    0    1
    Part Time    1    0    2    3
    
    print (df.columns)
    Index(['NSW', 'QLD', 'VIC', 'All'], dtype='object')
    

    If you need both column names, use list comprehension:

    df.columns = ['_'.join(col) for col in df.columns]
    print (df)
               basic_amt_NSW  basic_amt_QLD  basic_amt_VIC  basic_amt_All
    All                    1              1              2              4
    Full Time              0              1              0              1
    Part Time              1              0              2              3
    
    print (df.columns)
    Index(['basic_amt_NSW', 'basic_amt_QLD', 'basic_amt_VIC', 'basic_amt_All'], dtype='object')
    
    0 讨论(0)
提交回复
热议问题