I am trying to subset a dataframe using a multilevel index. For example:
df = pd.DataFrame({'state': ['CA', 'WA', 'CO', 'AZ'] * 3,
'office_id': range(1, 7) * 2,
'sales': [np.random.randint(100000, 999999)
for _ in range(12)]})
df2=df.groupby(['state', 'office_id']).agg({'sales': 'sum'})
sales
state office_id
AZ 2 839507
4 373917
6 347225
CA 1 798585
3 890850
5 454423
CO 1 819975
3 202969
5 614011
WA 2 163942
4 369858
6 959285
As you can see, df2 contains multilevel index with state and office_id. For df2, I would like to subset the dataframe by using the multindex find the following:
1) only state = AZ
2) only office_id <4
3) state = CA and office_id = 5
Historically I would rest the index in the dataframe and subset by columns, but that is not efficient.
Can someone please point me in the right direction? Thank you!