python - 如何在数据框中选择一个(或多个)日期
问题描述
我的数据框信息如下。我想创建另一个仅选择日期 = 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
解决方案
您可以尝试以下代码以仅选择指定的日期:
#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
推荐阅读
- amazon-web-services - 使用 Lambda 函数通过 Python 读取某个 Cloudwatch 警报状态及其指标
- ssh - ssh 向远程发送命令时如何获取 ~/.bashrc
- python - 如何将熊猫数据框写入现有的 xlsx 文件?
- macos - 开发板 NodeMCU 1.0 (ESP-12E Module) 编译时出错(读取 10 次)
- javascript - Redux-persist for reactJS 给出错误:Firefox 78.0.2 中的 noop 存储
- javascript - Angular html中的Highcharts
- vue.js - 使用 yarn install 时 Nuxt/Vuetify 的原色是黑白(附截图)
- python - 从字典键及其值生成元组
- c# - Add-Migration MyModel 提供空的 Up() Down() 方法
- vue.js - Vue beforeEach() 没有重定向到所需的页面