Selecting rows from a Pandas dataframe with a compound (hierarchical) index

后端 未结 3 1010
[愿得一人]
[愿得一人] 2020-12-07 12:05

I\'m suspicious that this is trivial, but I yet to discover the incantation that will let me select rows from a Pandas dataframe based on the values of a hierarchical key. S

3条回答
  •  误落风尘
    2020-12-07 12:32

    Try using xs to be very precise:

    In [5]: df.xs('a', level=0)
    Out[5]: 
            value1  value2
    group2                
    c          1.1     7.1
    c          2.0     8.0
    d          3.0     9.0
    
    In [6]: df.xs('c', level='group2')
    Out[6]: 
            value1  value2
    group1                
    a          1.1     7.1
    a          2.0     8.0
    

提交回复
热议问题