How to slice one MultiIndex DataFrame with the MultiIndex of another

前端 未结 3 1491
半阙折子戏
半阙折子戏 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:44

    Here is a way to get this slice:

    df.sort_index(inplace=True)
    idx = pd.IndexSlice
    df.loc[idx[:, ('foo','bar'), 'can'], :]
    

    yielding

               hi
    a b   c      
    1 bar can   3
      foo can   1
    2 bar can   7
      foo can   5
    3 bar can  11
      foo can   9
    

    Note that you might need to sort MultiIndex before you can slice it. Well pandas is kind enough to warn if you need to do it:

    KeyError: 'MultiIndex Slicing requires the index to be fully lexsorted tuple len (3), lexsort depth (1)'
    

    You can read more on how to use slicers in the docs

    If for some reason using slicers is not an option here is a way to get the same slice using .isin() method:

    df[df.index.get_level_values('b').isin(ix_use.get_level_values(0)) & df.index.get_level_values('c').isin(ix_use.get_level_values(1))]
    

    Which is clearly not as concise.

    UPDATE:

    For the conditions that you have updated here is a way to do it:

    cond1 = (df.index.get_level_values('b').isin(['foo'])) & (df.index.get_level_values('c').isin(['can']))
    cond2 = (df.index.get_level_values('b').isin(['bar'])) & (df.index.get_level_values('c').isin(['baz']))
    df[cond1 | cond2]
    

    producing:

               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
    

提交回复
热议问题