Home > Software design >  How do i shorten a pandas data frame based by the dates in one column
How do i shorten a pandas data frame based by the dates in one column

Time:10-18

So I have this data frame that has many columns, but I'm only interested in the data spanning from say 01/01/2009-01/01/2019 so I want to keep all the data in that range and get rid of everything else

CodePudding user response:

Assuming date column name as date_column

df_new = df[(df['date_column'] > '01/01/2009') & (df['date_column'] <= '01/01/2019')]
print(df_new)

CodePudding user response:

If they're correctly formatted:

df_new = df[df['date_col'].between('2009-01-01', '2019-01-01')]

CodePudding user response:

this will work no for any date format, dd-mm-yyyy or yyyy-mm-dd

df[(pd.to_datetime(df['Date']).dt.year >= 2009) & (pd.to_datetime(df['Date']).dt.year <= 2019)]
  • Related