我有以下数据库,我如何通过retailer_name,visit_number,visit_date选择行?
category_name Blades & Razors & Foam Diaper
retailer_name store_number visit_date
ABest 1177 2016-03-01 06:10:49 86 191
2016-03-24 08:59:33 129 222
2016-03-29 04:34:36 114 323
2016-04-12 10:56:26 225 235
1182 2016-03-02 08:54:00 161 217
例如,我想获取'ABest'
,1182
,2016-03-02 08:54:00
由于
答案 0 :(得分:2)
import pandas as pd
df = pd.DataFrame({'Blades & Razors & Foam': {('ABest', 1177, pd.Timestamp('2016-03-01 06:10:49')): 86, ('ABest', 1177, pd.Timestamp('2016-03-29 04:34:36')): 114, ('ABest', 1177, pd.Timestamp('2016-03-24 08:59:33')): 129, ('ABest', 1177, pd.Timestamp('2016-04-12 10:56:26')): 225, ('ABest', 1182, pd.Timestamp('2016-03-02 08:54:00')): 161}, 'Diaper': {('ABest', 1177, pd.Timestamp('2016-03-01 06:10:49')): 191, ('ABest', 1177, pd.Timestamp('2016-03-29 04:34:36')): 323, ('ABest', 1177, pd.Timestamp('2016-03-24 08:59:33')): 222, ('ABest', 1177, pd.Timestamp('2016-04-12 10:56:26')): 235, ('ABest', 1182, pd.Timestamp('2016-03-02 08:54:00')): 217}})
df.columns.names=[u'category_name']
df.index.names=[u'retailer_name', u'store_number', u'visit_date']
print (df)
category_name Blades & Razors & Foam Diaper
retailer_name store_number visit_date
ABest 1177 2016-03-01 06:10:49 86 191
2016-03-24 08:59:33 129 222
2016-03-29 04:34:36 114 323
2016-04-12 10:56:26 225 235
1182 2016-03-02 08:54:00 161 217
idx = pd.IndexSlice
print (df.loc[idx['ABest',1182,'2016-03-02 08:54:00'],:])
category_name Blades & Razors & Foam Diaper
retailer_name store_number visit_date
ABest 1182 2016-03-02 08:54:00 161 217
编辑:
如果您需要更改级别visit_date
to_datetime
,则可以使用:
import pandas as pd
df = pd.DataFrame({'Blades & Razors & Foam': {('ABest', 1182, '2016-03-02 08:54:00'): 161, ('ABest', 1177, '2016-04-12 10:56:26'): 225, ('ABest', 1177, '2016-03-01 06:10:49'): 86, ('ABest', 1177, '2016-03-24 08:59:33'): 129, ('ABest', 1177, '2016-03-29 04:34:36'): 114}, 'Diaper': {('ABest', 1182, '2016-03-02 08:54:00'): 217, ('ABest', 1177, '2016-04-12 10:56:26'): 235, ('ABest', 1177, '2016-03-01 06:10:49'): 191, ('ABest', 1177, '2016-03-24 08:59:33'): 222, ('ABest', 1177, '2016-03-29 04:34:36'): 323}})
df.index.names=[u'retailer_name', u'store_number', u'visit_date']
print (df)
Blades & Razors & Foam Diaper
retailer_name store_number visit_date
ABest 1177 2016-03-01 06:10:49 86 191
2016-03-24 08:59:33 129 222
2016-03-29 04:34:36 114 323
2016-04-12 10:56:26 225 235
1182 2016-03-02 08:54:00 161 217
df.reset_index(inplace=True)
df['visit_date'] = pd.to_datetime(df['visit_date'])
df.set_index(['retailer_name','store_number','visit_date'], inplace=True)
idx = pd.IndexSlice
print (df.loc[idx['ABest',1182,'2016-03-02 08:54:00'],:])
Blades & Razors & Foam Diaper
retailer_name store_number visit_date
ABest 1182 2016-03-02 08:54:00 161 217