Josh Josh - 1 year ago 94
Python Question

filter pandas dataframe for past x days

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.

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


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


Answer Source

Try this:

import datetime
import pandas as pd 

df[ > - pd.to_timedelta("30day")]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download