python - selecting secondary index by tuple of labels -
i have sales data indexed on ('dt', 'product_id')
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 # goes on , on...
how can view income of product 10016
, 10025
in between 2015-01-15
, 2015-01-16
? tried learn pandas slicers here couldn't right:
in [44]: sub.loc[idx[start:end,[10016,10018]]] keyerror: 'none of [[10055, 10158]] in [columns]'
raw data
import pandas pd product_order = pd.dataframe.from_csv('order.csv') odr = product_order.set_index(['dt','product_id'])
dt,product_id,subsidy 2015-03-03 00:39:08+08:00,10029,50.00 2015-03-09 00:47:00+08:00,10016,55.00 2015-03-13 01:00:12+08:00,10029,23.00 2015-03-15 01:03:40+08:00,10016,21.00 2015-03-16 02:18:45+08:00,10016,52.00
assuming here gp
groupby object can slice following:
in [146]: idx = pd.indexslice gp.loc[idx['2015-01-15':'2015-01-16'], idx[10016:10025]] out[146]: dt product_id 2015-01-15 10016 23 10017 188 10018 nan 2015-01-16 10016 188 name: income, dtype: float64
so need define indexslice
each level want perform row selection criteria on
Comments
Post a Comment