I am lost in a sea of ix, xs, MultiIndex, get_level_values and other Pandas.
I have a Series with a 3-level multi-index. What is an efficient way to slice my Series based on values at the different levels?
My Series looks like this:
days id start_date
0 S0036-4665(00)04200108 2013-05-18 1
3 S0036-4665(00)04200108 2013-05-18 1
5 S0036-4665(00)04200108 2013-05-18 3
13 S0036-4665(00)04200108 2013-05-18 1
19 S0036-4665(00)04200108 2013-05-18 1
39 S0036-4665(00)04200108 2013-05-18 1
...
Obviously the values of id and start_date vary as you go down the fame
I would like to be able to slice based on the following: - days within numeric range - id within a specific set - start_date within a specific date range
So far, I found this solution, which suggests using df[df.index.get_level_values('a').isin([5, 7, 10, 13])]
, and I figured out that I can do:
s.select(lambda x: x[0] < 20 and (x[1] in set('some id', 'other id') ))
Are either of those the best solution? I felt that I should be able to do something with xs, or ix, but the former seems to only let you filter by a specific value, and the latter only indexes on the position in the series?