I am working on a problem where I have to take user input which is an integer indicating the number of months I have to look back at. For example if I want to look at the data 3 months back I must take input from user as 3. Based on this integer user input I have to filter my dataset. For example today’s date is 8/30/2022 so 3 months back will be 5/30/2022. Now I want to filter my dataframe to include only those rows for this date which is 3 months back i.e. 5/30/2022
I tried using datetime and relativetime libraries but nothing seems to work for me.
Below is an example of my dataframe:
so I want output to be rows corresponding to 1 and 2
Advertisement
Answer
You can use the DateOffset
function in Pandas. See the documentation for more details.
An example of this is below which assumes you have a data frame with a date column:
num_months = int(input('Please enter the number of months to look back: ')) df['date'] = pd.to_datetime(df['date']) past_date = pd.to_datetime('today') - pd.DateOffset(months=num_months) df = df[df['date'] >= past_date] print(df)
The above will filter the data frame on the date
column leaving only those rows where the date is on or after the calculated date i.e. today’s date minus the specified number of months.