How to select a subset from a Multi-Index Datafram

2019-07-28 20:47发布

问题:

I have a dataframe like below:

                     dates         0
numbers letters               
0       a       2013-01-01  0.261092
                2013-01-02 -1.267770
                2013-01-03  0.008230
        b       2013-01-01 -1.515866
                2013-01-02  0.351942
                2013-01-03 -0.245463
        c       2013-01-01 -0.253103
                2013-01-02 -0.385411
                2013-01-03 -1.740821
1       a       2013-01-01 -0.108325
                2013-01-02 -0.212350
                2013-01-03  0.021097
        b       2013-01-01 -1.922214
                2013-01-02 -1.769003
                2013-01-03 -0.594216
        c       2013-01-01 -0.419775
                2013-01-02  1.511700
                2013-01-03  0.994332
2       a       2013-01-01 -0.020299
                2013-01-02 -0.749474
                2013-01-03 -1.478558
        b       2013-01-01 -1.357671
                2013-01-02  0.161185
                2013-01-03 -0.658246
        c       2013-01-01 -0.564796
                2013-01-02 -0.333106
                2013-01-03 -2.814611

Now I was given a list like:

   numbers letters
0        0       b
1        1       c

I need to select data whose indexs satisfy the list. The answer is like:

                     dates         0
numbers letters               
0       b       2013-01-01 -1.515866
                2013-01-02  0.351942
                2013-01-03 -0.245463
1       c       2013-01-01 -0.419775
                2013-01-02  1.511700
                2013-01-03  0.994332

How can I select the specific data from the Dataframe of MultiIndex?

回答1:

You can also use index intersection:

In [39]: l
Out[39]:
   numbers letters
0        0       b
1        1       c


In [40]: df.loc[df.index.intersection(l.set_index(['numbers','letters']).index)]
Out[40]:
                      dates         0
numbers letters
0       b        2013-01-01 -1.515866
        b        2013-01-02  0.351942
        b        2013-01-03 -0.245463
1       c        2013-01-01 -0.108325
        c        2013-01-02 -0.212350
        c        2013-01-03  0.021097
        c        2013-01-01 -0.419775
        c        2013-01-02  1.511700
        c        2013-01-03  0.994332

Or more straightforward and faster solution from @Javier:

In [155]: df.loc[l.set_index(['numbers','letters']).index]
Out[155]:
                      dates         0
numbers letters
0       b        2013-01-01 -1.515866
        b        2013-01-02  0.351942
        b        2013-01-03 -0.245463
1       c        2013-01-01 -0.108325
        c        2013-01-02 -0.212350
        c        2013-01-03  0.021097
        c        2013-01-01 -0.419775
        c        2013-01-02  1.511700
        c        2013-01-03  0.994332

Timing:

for 27.000 rows Multi-Index DF

In [156]: df = pd.concat([df.reset_index()] * 10**3, ignore_index=True).set_index(['numbers','letters'])

In [157]: df.shape
Out[157]: (27000, 2)

In [158]: %%timeit
     ...: q = l.apply(lambda r: "(numbers == {} and letters == '{}')".format(r.numbers, r.letters),
     ...:             axis=1) \
     ...:      .str.cat(sep=' or ')
     ...: df.query(q)
     ...:
10 loops, best of 3: 21.3 ms per loop

In [159]: %%timeit
     ...: df.loc[l.set_index(['numbers','letters']).index]
     ...:
10 loops, best of 3: 20.2 ms per loop

In [160]: %%timeit
     ...: df.loc[df.index.intersection(l.set_index(['numbers','letters']).index)]
     ...:
10 loops, best of 3: 27.2 ms per loop

for 270.000 rows Multi-Index DF

In [163]: %%timeit
     ...: q = l.apply(lambda r: "(numbers == {} and letters == '{}')".format(r.numbers, r.letters),
     ...:             axis=1) \
     ...:      .str.cat(sep=' or ')
     ...: df.query(q)
     ...:
10 loops, best of 3: 117 ms per loop

In [164]: %%timeit
     ...: df.loc[l.set_index(['numbers','letters']).index]
     ...:
1 loop, best of 3: 142 ms per loop

In [165]: %%timeit
     ...: df.loc[df.index.intersection(l.set_index(['numbers','letters']).index)]
     ...:
10 loops, best of 3: 185 ms per loop

Conclusion: df.query() method which uses numexpr module internaly seems to be faster for bigger DFs



回答2:

assuming you have the following DF with the values that you want to get:

In [28]: l
Out[28]:
   numbers letters
0        0       b
1        1       c

if you need to select all rows where numbers is either 0 or 1 and letters is in ['b','c'] you can use df.query() method as follows:

In [29]: df.query("numbers in @l.numbers and letters in @l.letters")
Out[29]:
                      dates         0
numbers letters
0       b        2013-01-01 -1.515866
        b        2013-01-02  0.351942
        b        2013-01-03 -0.245463
        c        2013-01-01 -0.253103
        c        2013-01-02 -0.385411
        c        2013-01-03 -1.740821
1       c        2013-01-01 -0.108325
        c        2013-01-02 -0.212350
        c        2013-01-03  0.021097
        b        2013-01-01 -1.922214
        b        2013-01-02 -1.769003
        b        2013-01-03 -0.594216
        c        2013-01-01 -0.419775
        c        2013-01-02  1.511700
        c        2013-01-03  0.994332

or simply:

df.query("numbers in [0,1] and letters in ['b','c']")

UPDATE: if it must be exact matches, like (0, 'b') and (1, 'c'):

In [14]: q = l.apply(lambda r: "(numbers == {} and letters == '{}')".format(r.numbers, r.letters),
    ...:             axis=1) \
    ...:      .str.cat(sep=' or ')
    ...:

In [15]: q
Out[15]: "(numbers == 0 and letters == 'b') or (numbers == 1 and letters == 'c')"

In [16]: df.query(q)
Out[16]:
                      dates         0
numbers letters
0       b        2013-01-01 -1.515866
        b        2013-01-02  0.351942
        b        2013-01-03 -0.245463
1       c        2013-01-01 -0.108325
        c        2013-01-02 -0.212350
        c        2013-01-03  0.021097
        c        2013-01-01 -0.419775
        c        2013-01-02  1.511700
        c        2013-01-03  0.994332