[英]pandas: slice on MultiIndex by range of first index, and choose secondary index label
我有一些按('dt', 'product_id')
分組的銷售數據('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...
我怎么可以查看產品的收入10016
之間2015-01-15
和2015-01-16
,這是最好的,我可以直接切片使用給定的多指標這個巨大的數據幀,導致數據是比較大的。
.xs()
是在多級索引中進行切片的好方法。 然后.loc
進一步提取所需的所需日期間隔。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.