Select only one index of multiindex DataFrame

前端 未结 2 1398
执笔经年
执笔经年 2020-12-05 01:58

I am trying to create a new DataFrame using only one index from a multi-indexed DataFrame.

                   A         B         C
first second                    


        
相关标签:
2条回答
  • 2020-12-05 02:41

    The solution is fairly new and uses the df.xs function as

    In [88]: df.xs('bar', level='first')
    Out[88]:
    Second  Third
    one     A       -2.315312
            B        0.497769
            C        0.108523
    two     A       -0.778303
            B       -1.555389
            C       -2.625022
    dtype: float64
    

    Can also do with multiple indices as

    In [89]: df.xs(('bar', 'A'), level=('First', 'Third'))
    Out[89]:
    Second
    one   -2.315312
    two   -0.778303
    dtype: float64
    

    The setup for the examples is below

    import pandas as pd
    import numpy as np
    arrays = [
        np.array(['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux']),
        np.array(['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'])
    ]
    index = pd.MultiIndex.from_tuples(list(zip(*arrays)), names=['first', 'second'])
    df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)
    df.index.names = pd.core.indexes.frozen.FrozenList(['First', 'Second', 'Third'])
    df = df.unstack()
    
    0 讨论(0)
  • 2020-12-05 02:51

    One way could be to simply rebind df.index to the desired level of the MultiIndex. You can do this by specifying the label name you want to keep:

    df.index = df.index.get_level_values('first')
    

    or use the level's integer value:

    df.index = df.index.get_level_values(0)
    

    All other levels of the MultiIndex would disappear here.

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