pandas: Boolean indexing with multi index

前端 未结 6 1748
再見小時候
再見小時候 2021-02-08 14:52

There are many questions here with similar titles, but I couldn\'t find one that\'s addressing this issue.

I have dataframes from many different origins, and I want to f

6条回答
  •  半阙折子戏
    2021-02-08 15:13

    You can use pd.IndexSlicer.

    >>> df.loc[pd.IndexSlice[filt[filt].index.values, :], :]
         c
    a b   
    1 1  0
      2  1
      3  2
    3 1  6
      2  7
      3  8
    

    where filt[filt].index.values is just [1, 3]. In other words

    >>> df.loc[pd.IndexSlice[[1, 3], :]]
         c
    a b   
    1 1  0
      2  1
      3  2
    3 1  6
      2  7
      3  8
    

    so if you design your filter construction a bit differently, the expression gets shorter. The advantave over Emanuele Paolini's solution df[filt[df.index.get_level_values('a')].values] is, that you have more control over the indexing.

    The topic of multiindex slicing is covered in more depth here.

    Here the full code

    import pandas as pd
    import numpy as np
    df = pd.DataFrame({'a':[1,1,1,2,2,2,3,3,3], 'b':[1,2,3,1,2,3,1,2,3], 'c':range(9)}).set_index(['a', 'b'])
    filt = pd.Series({1:True, 2:False, 3:True})
    
    print(df.loc[pd.IndexSlice[[1, 3], :]])
    print(df.loc[(df.index.levels[0].values[filt], slice(None)), :])
    print(df.loc[pd.IndexSlice[filt[filt].index.values, :], :])
    

提交回复
热议问题