How can I reorder multi-indexed dataframe columns at a specific level

后端 未结 4 1276
时光取名叫无心
时光取名叫无心 2020-12-17 08:25

I have a multi-indexed DataFrame with names attached to the column levels. I\'d like to be able to easily shuffle the columns around so that they match the ord

相关标签:
4条回答
  • 2020-12-17 09:16

    The comment by andrew_reece should be the accepted answer. Simply use reindex().

    Copy & pasting from the github issue:

    >>> df
                         vals
    first second third       
    mid   3rd    992     1.96
                 562    12.06
          1st    73     -6.46
                 818   -15.75
                 658     5.90
    btm   2nd    915     9.75
                 474    -1.47
                 905    -6.03
          1st    717     8.01
                 909   -21.12
          3rd    616    11.91
                 675     1.06
                 579    -4.01
    top   1st    241     1.79
                 363     1.71
          3rd    677    13.38
                 238   -16.77
                 407    17.19
          2nd    728   -21.55
                 36      8.09
    >>> df.reindex(['top', 'mid', 'btm'], level='first')
                         vals
    first second third       
    top   1st    241     1.79
                 363     1.71
          3rd    677    13.38
                 238   -16.77
                 407    17.19
          2nd    728   -21.55
                 36      8.09
    mid   3rd    992     1.96
                 562    12.06
          1st    73     -6.46
                 818   -15.75
                 658     5.90
    btm   2nd    915     9.75
                 474    -1.47
                 905    -6.03
          1st    717     8.01
                 909   -21.12
          3rd    616    11.91
                 675     1.06
                 579    -4.01
    >>> df.reindex(['1st', '2nd', '3rd'], level='second')
                         vals
    first second third       
    mid   1st    73     -6.46
                 818   -15.75
                 658     5.90
          3rd    992     1.96
                 562    12.06
    btm   1st    717     8.01
                 909   -21.12
          2nd    915     9.75
                 474    -1.47
                 905    -6.03
          3rd    616    11.91
                 675     1.06
                 579    -4.01
    top   1st    241     1.79
                 363     1.71
          2nd    728   -21.55
                 36      8.09
          3rd    677    13.38
                 238   -16.77
                 407    17.19
    >>> df.reindex(['top', 'btm'], level='first').reindex(['1st', '2nd'], level='second')
                         vals
    first second third       
    top   1st    241     1.79
                 363     1.71
          2nd    728   -21.55
                 36      8.09
    btm   1st    717     8.01
                 909   -21.12
          2nd    915     9.75
                 474    -1.47
                 905    -6.03
    
    0 讨论(0)
  • 2020-12-17 09:21

    I don't know of anything off-hand. Created an enhancement ticket about it:

    http://github.com/pydata/pandas/issues/1864

    0 讨论(0)
  • 2020-12-17 09:26

    There is a very simple way: just create a new dataframe based on the original, with the correct order of multiindex columns:

    multi_tuples = [('IWWGCW',24), ('IWWGCW',48), ('IWWGDW',24), ('IWWGDW',48)
        , ('BASE',24), ('BASE',48)]
    
    multi_cols = pd.MultiIndex.from_tuples(multi_tuples, names=['Experiment', 'Lead Time'])
    
    df_ordered_multi_cols = pd.DataFrame(df_ori, columns=multi_cols)
    
    0 讨论(0)
  • 2020-12-17 09:29

    This is the simplest one that worked for me:

    1 - for your selected level, create a list with columns in desired order;

    2 - reindex your columns and create a MultiIndex object from that list, keep in mind this returns a tuple;

    3 - use the MultiIndex object to reorder your DataFrame.

    cols = ['IWWGCW', 'IWWGDW', 'BASE']
    
    new_cols = df.columns.reindex(cols, level = 0)
    
    df.reindex(columns= new_cols[0]) #new_cols is a single item tuple
    

    in one line:

    df.reindex(columns= df.columns.reindex(['IWWGCW', 'IWWGDW', 'BASE'], 
    level = 0)[0])
    

    voilá

    0 讨论(0)
提交回复
热议问题