Pandas: Multilevel column names

前端 未结 5 1123
梦毁少年i
梦毁少年i 2020-12-08 02:28

pandas has support for multi-level column names:

>>>  x = pd.DataFrame({\'instance\':[\'first\',\'first\',\'first\'],\'foo\':[\'a\',\'b         


        
5条回答
  •  伪装坚强ぢ
    2020-12-08 03:24

    You can use concat. Give it a dictionary of dataframes where the key is the new column level you want to add.

    In [46]: d = {}
    
    In [47]: d['first_level'] = pd.DataFrame(columns=['idx', 'a', 'b', 'c'],
                                             data=[[10, 0.89, 0.98, 0.31],
                                                   [20, 0.34, 0.78, 0.34]]).set_index('idx')
    
    In [48]: pd.concat(d, axis=1)
    Out[48]:
        first_level
                  a     b     c
    idx
    10         0.89  0.98  0.31
    20         0.34  0.78  0.34
    

    You can use the same technique to create multiple levels.

    In [49]: d['second_level'] = pd.DataFrame(columns=['idx', 'a', 'b', 'c'],
                                              data=[[10, 0.29, 0.63, 0.99],
                                                    [20, 0.23, 0.26, 0.98]]).set_index('idx')
    
    In [50]: pd.concat(d, axis=1)
    Out[50]:
        first_level             second_level
                  a     b     c            a     b     c
    idx
    10         0.89  0.98  0.31         0.29  0.63  0.99
    20         0.34  0.78  0.34         0.23  0.26  0.98
    

提交回复
热议问题