I have dataframe df
with daily stock market for 10 years having columns Date
, Open
, Close
.
I want to calculate the change between any two consecutive values (in Close
) as a ratio of the previous value.
For example, in photo below, first entry (-0.0002) for Interday_return
is calculated as = (43.06-43.07)/43.07.
Similarly the next value 0.0046 is calculated as = (43.26-43.06)/43.06.
And so on..
I am able to create a new column Interday_Close_change
which is basically the difference between each 2 consecutive rows using this code (ie.. finding the numerator of the above mentioned fraction). However, I dont know how to divide any element in Interday_Close_change
by value in the preceding row and get a new column Interday_return
.
df = pd.DataFrame(data, columns=columns)
df['Interday_Close_change'] = df['Close'].astype(float).diff()
df.fillna('', inplace=True)