Python 用多索引选择行

Python 用多索引选择行,python,pandas,dataframe,multi-index,Python,Pandas,Dataframe,Multi Index,我有以下数据库,如何按零售商名称、访问编号、访问日期选择行 category_name Blades & Razors & Foam Diaper retailer_name store_number visit_date ABest 1177 2

我有以下数据库,如何按零售商名称、访问编号、访问日期选择行

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

谢谢

我想你可以使用
切片器
-:

编辑:

如果您需要更改级别
访问日期
,您可以使用:

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

你真的应该使用熊猫吗?我得到了一个关键错误:('ABest',1182,Timestamp('2016-03-02 08:54:00'))嗯,我没有datetimeindex,请给我一点时间。有趣的是,对我来说它工作得很好。你的熊猫版本是什么<代码>打印pd.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。
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