我的数据框信息如下。 我想创建另一个仅选择date = 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
答案 0 :(得分:0)
您可以尝试以下代码以仅选择指定的日期:
#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