How to apply hierarchy or multi-index to pandas columns

前端 未结 1 437
情书的邮戳
情书的邮戳 2021-02-13 19:59

I have seen lots of examples on how to arrange dataframe row indexes hierarchically, but I am trying to do the same for columns and am not understanding the syntax:

Give

相关标签:
1条回答
  • 2021-02-13 20:38

    You can define MultiIndices using the from_arrays or from_tuples or from_product classmethods. Here is an example using from_arrays:

    arrays = [[1, 2]*3, ['A', 'B', 'C']*2]
    columns = pd.MultiIndex.from_arrays(arrays, names=['foo', 'bar'])
    
    df = pd.DataFrame(np.random.randn(2,6),
                      columns=columns,
                      index= pd.date_range('20000103',periods=2))
    

    yields

    In [81]: df
    Out[81]: 
    foo                1         2         1         2         1         2
    bar                A         B         C         A         B         C
    2000-01-03  1.277234 -0.899547  0.040337 -0.878752 -0.524336  0.922440
    2000-01-04 -1.706797  0.450379  1.510868 -2.539827 -1.909996 -0.003851
    

    Defining a MultiIndex for the index is done in exactly the same way as for columns.

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