过滤过去x天的 pandas 数据框 [英] filter pandas dataframe for past x days

查看:69
本文介绍了过滤过去x天的 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有日期列的数据框,该列每天都会更新.我只想用过去30天的数据创建一个副本.

I have a dataframe with a date column that I update daily. I'd like to create a copy of it with just the past 30 day's of data.

我根据对R的了解了解了以下语法:

I tried the following syntax based on what I know about doing this in R:

df[df[date]>dt.date.today()-30]

日期列不是索引,但我不反对这样做,如果有帮助的话!

The date column is not the index but I'm not opposed to making it so if that helps!

谢谢!

推荐答案

尝试一下:

import datetime
import pandas as pd 


df[df.the_date_column > datetime.datetime.now() - pd.to_timedelta("30day")]

更新:根据乔什的建议进行编辑.

Update: Edited as suggested by Josh.

这篇关于过滤过去x天的 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆