filter a df by all the values with dates before today's date
Question:
I’d like to filter a df
by date. But I would like all the values with any date before today’s date (python).
For example from the table below, I’d like the rows that have a date before today’s date (i.e. row 1 to row 3).
ID
date
1
2022-03-25 06:00:00
2
2022-04-25 06:00:00
3
2022-05-25 06:00:00
4
2022-08-25 06:00:00
Thanks
Answers:
You can try this?
from datetime import datetime
df[df['date'] < datetime.now()]
Output:
ID date
0 1 2022-03-25 06:00:00
1 2 2022-04-25 06:00:00
2 3 2022-05-25 06:00:00
This will work
#convert column to datetime object
df['date'] = pd.to_datetime(df['date'], infer_datetime_format=True, errors='coerce')
#filter column
df.loc[df['date'] < datetime.now()]
I’d like to filter a df
by date. But I would like all the values with any date before today’s date (python).
For example from the table below, I’d like the rows that have a date before today’s date (i.e. row 1 to row 3).
ID | date |
---|---|
1 | 2022-03-25 06:00:00 |
2 | 2022-04-25 06:00:00 |
3 | 2022-05-25 06:00:00 |
4 | 2022-08-25 06:00:00 |
Thanks
You can try this?
from datetime import datetime
df[df['date'] < datetime.now()]
Output:
ID date
0 1 2022-03-25 06:00:00
1 2 2022-04-25 06:00:00
2 3 2022-05-25 06:00:00
This will work
#convert column to datetime object
df['date'] = pd.to_datetime(df['date'], infer_datetime_format=True, errors='coerce')
#filter column
df.loc[df['date'] < datetime.now()]