Selecting columns from pandas MultiIndex

前端 未结 7 966
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 22:36

I have DataFrame with MultiIndex columns that looks like this:

# sample data
col = pd.MultiIndex.from_arrays([[\'one\', \'one\', \'one\', \'two\', \'two\', \         


        
相关标签:
7条回答
  • 2020-11-29 23:20

    To select all columns named 'a' and 'c' at the second level of your column indexer, you can use slicers:

    >>> data.loc[:, (slice(None), ('a', 'c'))]
    
            one                 two          
              a         c         a         c
    0 -0.983172 -2.495022 -0.967064  0.124740
    1  0.282661 -0.729463 -0.864767  1.716009
    2  0.942445  1.276769 -0.595756 -0.973924
    3  2.182908 -0.267660  0.281916 -0.587835
    

    Here you can read more about slicers.

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