1

I have a pandas dataframe with a multiindex (module_id, id). I'm trying to select all row less than or equal to 2.2 (module_id.id)

                calculated
module_id id
1       1           False
        2           False
        3           False
2       1           True
        2           True
        3           True

How would you do this?

a_b
  • 1,828
  • 5
  • 23
  • 37
  • Where's 2.2 in your DataFrame? – cs95 Jan 30 '19 at 21:39
  • 1
    Perhaps [Select rows in pandas MultiIndex DataFrame](https://stackoverflow.com/questions/53927460/how-do-i-slice-or-filter-multiindex-dataframe-levels/53927461#53927461) might be useful. – cs95 Jan 30 '19 at 21:40
  • 1
    So long as your index is lexsorted (can check with `df.index.is_lexsorted()`) the normal slicing works: `df[:(2,2)]` – ALollz Jan 30 '19 at 21:40

2 Answers2

0

I assume that you want to select records bigger or equal than 2, 2 (2 in first and second column) so you have to do:

df.query('module_id >= 2 & id >= 2')
Hubert Dudek
  • 1,666
  • 1
  • 13
  • 21
0

(based on @ALollz comment, which is the right answer)

df[:(2,2)]

a_b
  • 1,828
  • 5
  • 23
  • 37