Home > database >  pandas selecting rows for specific time period
pandas selecting rows for specific time period

Time:06-09

I have a pandas dataframe with date index. Like this

                  A         B        C
     date
  2021-04-22      2         1        3
  2021-05-22      3         2        4 
  2021-06-22      4         3        5
  2021-07-22      5         4        6
  2021-08-22      6         5        7     

I want to create a new dataframe that selects rows that are only for 2 days previous for a given date. So for example if I give selected = '2021-08-22', what I need is a new dataframe like below

                  A         B        C
     date
  2021-07-22      5         4        6
  2021-08-22      6         5        7

can someone please help me with this? Many thanks for your help

CodePudding user response:

I'm assuming that you meant months instead of days.

You can use the df.apply method in order to filter the dataframe rows with a function.

Here is a function that received the inputs you described and returns the new dataframe:

Working example

def filter_df(df, date, num_months):
    def diff_month(row):
        date1 = datetime.strptime(row["date"], '%Y-%m-%d')
        date2 = datetime.strptime(date, '%Y-%m-%d')
        return ((date1.year - date2.year) * 12   date1.month - date2.month)

    return df[df.apply(diff_month, axis=1) > - num_months]


print(filter_df(df, "2021-08-22", 2))

CodePudding user response:

You can convert convert the index to DatetimeIndex, then use df[start_date : end_date]

df.index = pd.to_datetime(df.index)
selected = '2021-08-22'
res = df[(pd.to_datetime(selected)-pd.Timedelta(days=2)) : selected]
print(res)

            A  B  C
2021-08-22  6  5  7
  • Related