Using .loc with a MultiIndex in pandas?

后端 未结 2 1401
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-28 13:59

Does anyone know if it is possible to use the DataFrame.loc method to select from a MultiIndex? I have the following DataFrame and would like to be able to access the values

相关标签:
2条回答
  • 2020-12-28 14:27

    Try the cross-section indexing:

    In [68]: df.xs('at', level='QGram', drop_level=False).loc[[1,4]]
    Out[68]: 
            Char  Dwell  Flight  ND_Offset  Offset
    QGram                                         
    at    1    t    180       0   0.108363       5
          4    a     20     180   0.000000       0
    
    0 讨论(0)
  • 2020-12-28 14:37

    If you are on version 0.14, you can simply pass a tuple to .loc as below:

    df.loc[('at', [1,3,4]), 'Dwell']
    
    0 讨论(0)
提交回复
热议问题