通过标签元组选择二级索引

时间:2015-07-14 10:32:56

标签: python pandas

我在('dt', 'product_id')上编制了一些索引的销售数据,如下所示:

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...

如何在1001610025之间查看产品2015-01-152015-01-16的收入?我试图了解大熊猫切片机here,但无法做到正确:

In [44]: sub.loc[idx[start:end,[10016,10018]]]

KeyError: 'None of [[10055, 10158]] are in the [columns]'

原始数据

import pandas as 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

1 个答案:

答案 0 :(得分:1)

假设此处gp已成为您的groupby对象,您可以按以下方式进行切片:

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

因此,您需要为要在其上执行行选择标准的每个级别定义IndexSlice

相关问题