I have a function that gets the stock price (adjusted closing price) on a specific date of the format DD-MM-YYYY. I have a Pandas Dataframe that looks like the following, with a column for date, as well as the stock price calculated using said function.
Date Stock price Percent change
0 02-07-2022 22.09
1 06-04-2022 18.22
2 01-01-2022 16.50
3 30-09-2021 18.15
4 03-07-2021 17.96
I need to calculate the percent change, which is calculated by taking (new/old - 1)*100, so in the top cell it would say (22.09/18.22 - 1)*100 = 21.24039517 because the stock increased 21.2% between 06-04-2022 and 02-07-2022.
So I need to "reference" the row below when applying a function, meanwhile I still reference the current row, because I need both to calculate change. For the bottom one, it can just be NaN or similar. Any suggestions?
CodePudding user response:
I would first sort on date (given that that column is already datetime
):
df = df.sort_values(by='Date', ascending=True)
And then calculate the percentage change and fill NaN
with 0
, or with something else if you prefer:
df["Percent change"] = df["Stock price"].pct_change(periods=1).fillna(0)