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

后端 未结 4 1285
时光取名叫无心
时光取名叫无心 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: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)
    

提交回复
热议问题