Search code examples
pythonpandasdataframesklearn-pandas

Move data from a column to seven days in advance - pandas Dataframe


I have a pandas Dataframe with 2 columns. One of them is the index in date format and the other one is a rate R (a number between 0 and 1). How can I add another column to the pandas Dataframe that contains the rate R for the one-week before day?

So at the end I have the dates, the rate of that day and the rate of the (day-7 days) of the DataFrame?


Solution

  • You can use pandas shift like this:

    df['newColumn'] = df['RateColumn'].shift(7)
    

    Keep in mind that first 7 values of the new column will be Nans as there are no data for them.