I want my loop to go through 3 dfs with the same format, and do 3 things:
- Transform "Date" object column to datetime.
- Use loc to filter only the dates after year 2021.
- Set date as index.
Say I have 3 dataframes named df1, df2 and df3, they are all shaped like this:
| Index | Date | Information |
|---|---|---|
| 1 | 2020-01-01 | Blablabla |
| 2 | 2021-01-01 | Blablabla |
| 3 | 2022-01-01 | Blablabla |
After running my code, I want all dfs to be shaped like this:
| Date | Information |
|---|---|
| 2021-01-01 | Blablabla |
| 2022-01-01 | Blablabla |
The code I'm running:
dfs = [df1, df2, df3]
for i in dfs:
i['Date'] = pd.to_datetime(i['Date'])
i = i.loc[i['Date'].dt.year >= 2021]
i.set_index('Date', inplace=True)
I can't seem to get it to work. The first part of the loop is working, it transformms to datetime, but the filter is not working neither the indexing.