通过date_range从DataFrame中选择行

时间:2016-08-01 12:58:14

标签: python pandas

我有以下数据框:

       FAK_ART    FAK_DAT  LEIST_DAT      KD_CRM MW_BW       EQ_NR MATERIAL  \
0         ZPAF 2015-05-18 2015-05-31         TMD     E  1003507107  G230ETS   
1         ZPAF 2015-05-18 2015-05-31         TMD     B  1003507107  G230ETS   
2         ZPAF 2015-05-18 2015-05-31         TMD     E  1003507108  G230ETS   
3         ZPAF 2015-05-18 2015-05-31         TMD     B  1003507108  G230ETS   
4         ZPAF 2015-05-18 2015-05-31         TMD     E  1003507109  G230ETS   
5         ZPAF 2015-05-18 2015-05-31         TMD     B  1003507109  G230ETS   
6         ZPAF 2015-05-18 2015-05-31         TMD     E  1003507110  G230ETS   
7         ZPAF 2015-05-18 2015-05-31         TMD     B  1003507110  G230ETS   
8         ZPAF 2015-05-18 2015-05-31         TMD     E  1003507111  G230ETS 

。 。

387976    ZPAF 2016-02-12 2016-02-29  T-HOME ICP     B  1001022686   A60ETS   
387977    ZPAF 2016-02-12 2016-02-29  T-HOME ICP     B  1001022686   A60ETS   
387978    ZPAF 2016-02-12 2016-02-29  T-HOME ICP     E  1001022712   A60ETS   
387979    ZPAF 2016-02-12 2016-02-29  T-HOME ICP     B  1001022712   A60ETS   
387980    ZPAF 2016-02-12 2016-02-29  T-HOME ICP     E  1001022735   A60ETS   
387981    ZPAF 2016-02-12 2016-02-29  T-HOME ICP     B  1001022735   A60ETS   
387982    ZPAF 2016-02-12 2016-02-29  T-HOME ICP     B  1001022735   A60ETS   
387983    ZPAF 2016-02-12 2016-02-29  T-HOME ICP     E  1001022748   A60ETS   
387984    ZPAF 2016-02-12 2016-02-29  T-HOME ICP     B  1001022748   A60ETS   
387985    ZPAF 2016-02-12 2016-02-29  T-HOME ICP     E  1001022760   A60ETS

现在我想只选择日期为2015-05-31的行。

我尝试使用date_range处理它,但我总是遇到错误:

  

ValueError:值的长度与索引的长度

不匹配

  

ValueError:必须指定开始,结束或句点中的两个

我的想法是:

data_faktura['LEIST_DAT'] = pd.date_range('2016-01-31', '2016-01-31')

然后我收到错误!

我该如何解决或解决这个问题?

1 个答案:

答案 0 :(得分:3)

您可以LEIST_DATset_index,然后按ix选择:

#change 2016-02-29 to your datetime
data_fakture = data_fakture.set_index('LEIST_DAT').ix['2016-02-29']
print (data_fakture)
           FAK_ART     FAK_DAT      KD_CRM MW_BW       EQ_NR MATERIAL
LEIST_DAT                                                            
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022686   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022686   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     E  1001022712   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022712   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     E  1001022735   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022735   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022735   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     E  1001022748   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022748   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     E  1001022760   A60ETS

loc

data_fakture = data_fakture.set_index('LEIST_DAT').loc['2016-02-29']
print (data_fakture)
           FAK_ART     FAK_DAT      KD_CRM MW_BW       EQ_NR MATERIAL
LEIST_DAT                                                            
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022686   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022686   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     E  1001022712   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022712   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     E  1001022735   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022735   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022735   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     E  1001022748   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022748   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     E  1001022760   A60ETS

您也可以选择开始日期和结束日期:

data_fakture = data_fakture.set_index('LEIST_DAT').ix['2015-05-31':'2016-02-29']
print (data_fakture)
           FAK_ART     FAK_DAT      KD_CRM MW_BW       EQ_NR MATERIAL
LEIST_DAT                                                            
2015-05-31    ZPAF  2015-05-18         TMD     E  1003507107  G230ETS
2015-05-31    ZPAF  2015-05-18         TMD     B  1003507107  G230ETS
2015-05-31    ZPAF  2015-05-18         TMD     E  1003507108  G230ETS
2015-05-31    ZPAF  2015-05-18         TMD     B  1003507108  G230ETS
2015-05-31    ZPAF  2015-05-18         TMD     E  1003507109  G230ETS
2015-05-31    ZPAF  2015-05-18         TMD     B  1003507109  G230ETS
2015-05-31    ZPAF  2015-05-18         TMD     E  1003507110  G230ETS
2015-05-31    ZPAF  2015-05-18         TMD     B  1003507110  G230ETS
2015-05-31    ZPAF  2015-05-18         TMD     E  1003507111  G230ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022686   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022686   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     E  1001022712   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022712   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     E  1001022735   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022735   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022735   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     E  1001022748   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     B  1001022748   A60ETS
2016-02-29    ZPAF  2016-02-12  T-HOME ICP     E  1001022760   A60ETS