首页 > 解决方案 > 如何在数据框中选择一个(或多个)日期

问题描述

我的数据框信息如下。我想创建另一个仅选择日期 = 1997-5 的数据框。在 SAS 中,这将使用“where”命令来完成......你能帮忙吗?

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 264 entries, 0 to 263
Data columns (total 8 columns):
Dates        264 non-null datetime64[ns]

我试着做:

may_97=returns_full[returns_full['Dates']='1997-05']

但得到了SyntaxError: invalid syntax

标签: pythonpandas

解决方案


您可以尝试以下代码以仅选择指定的日期:

#suppose this is your first dataframe
N=60
df1=pd.DataFrame({'no': np.linspace(0, stop=N-1, num=N),
                 'date': pd.date_range(start='1997-4-1',periods=N, freq='D')})
print(df1)

#This is your second dataframe that is only select the dates that you want
df2=df1[(df1.date.map(lambda x: x.strftime('%Y-%m'))).str.match('1997-05')]
print(df2)

输出:

#first dataframe
      no       date
0    0.0 1997-04-01
1    1.0 1997-04-02
2    2.0 1997-04-03
3    3.0 1997-04-04
4    4.0 1997-04-05
5    5.0 1997-04-06
...
56  56.0 1997-05-27
57  57.0 1997-05-28
58  58.0 1997-05-29
59  59.0 1997-05-30

#second dataframe
      no       date
30  30.0 1997-05-01
31  31.0 1997-05-02
32  32.0 1997-05-03
...
57  57.0 1997-05-28
58  58.0 1997-05-29
59  59.0 1997-05-30

推荐阅读