Reputation: 1512
I have some sales data grouped by ('dt', 'product_id')
like this:
In [43]: sub.head()
Out[43]:
income
dt product_id
2015-01-15 10016 23
2015-01-15 10017 188
2015-01-15 10018 NaN
2015-01-16 10016 188
2015-01-17 10025 1000
# this goes on and on...
how can I view the income of product 10016
in between 2015-01-15
and 2015-01-16
, and it's best that I can just slice this huge dataframe using the given MultiIndex, cause the data is relatively large.
Upvotes: 0
Views: 336
Reputation: 24752
.xs()
is a good way to do slicing in multi-level index.
And .loc
to further extract the required date intervals you want.
import pandas as pd
# your df
# =======================
print(df)
Out[82]:
income
dt product_id
2015-01-15 10016 23
10017 188
10018 NaN
2015-01-16 10016 188
2015-01-17 10025 1000
# one possible way
# ==========================================
df.xs(10016, level='product_id').loc['2015-01-15':'2015-01-16']
Out[88]:
income
dt
2015-01-15 23
2015-01-16 188
Upvotes: 1