How to slice one MultiIndex DataFrame with the MultiIndex of another

前端 未结 3 1472
半阙折子戏
半阙折子戏 2020-12-05 19:27

I have a pandas dataframe with 3 levels of a MultiIndex. I am trying to pull out rows of this dataframe according to a list of values that correspond to two of the levels.

3条回答
  •  伪装坚强ぢ
    2020-12-05 19:53

    I would recommend the query() method just like in this Q&A.

    Simply using this, which I think is a more natural way to express:

    In [27]: df.query("(b == 'foo' and c == 'can') or (b == 'bar' and c == 'baz')")
    Out[27]: 
               hi
    a b   c      
    1 foo can   1
      bar baz   2
    2 foo can   5
      bar baz   6
    3 foo can   9
      bar baz  10
    

提交回复
热议问题