'How to copy value from one column to another column however be one row lower in Pandas

How would I have copy the last value from one column and copy this to another column in Pandas. I am coding a stock portfolio withdrawal strategy and I need to copy the ["Portfolio Remaining"] variable * ["Return"] from index number [1: End_date] as after the first year the portfolio after the withdrawal (["Portfolio Remaining"]) column needs to multiplied from index [1: End_Date]. So I want a column to copy another column however the value in the new column will be 1 row lower in the new column.

I have tried to use df(Equity_Value).loc[0] * (df["Return]) then use df3["Portfolio_Value"].loc[1:End_Date] * (df["Return"] + 1).cumprod() however the calculations were wrong and python said this method isn't suitable due to the slicing however I am unsure how to do this in Pandas. I can't find anything on the documentation or online resources on how to do this.



Solution 1:[1]

Assuming pd is a pandas DataFrame, the following works for shifting records by one row:

pd["new_column"] = pd["old_column"].shift(1)

details at https://pandas.pydata.org/docs/reference/api/pandas.Series.shift.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lytseeker