2

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)

enter image description here

Sinha
  • 431
  • 1
  • 5
  • 12
  • Does https://stackoverflow.com/questions/22081878/get-previous-rows-value-and-calculate-new-column-pandas-python answer your question? – Karl Knechtel May 15 '22 at 23:59

1 Answers1

0

This should do it:

df['Interday_Close_change'] = df['Close'].pct_change().fillna('')

Sample input:

       Date   Open  Close
0  1/2/2018  42.54  43.07
1  1/3/2018  43.13  43.06
2  1/4/2018  43.14  43.26
3  1/5/2018  43.36  43.75

Sample output:

       Date   Open  Close Interday_Close_change
0  1/2/2018  42.54  43.07
1  1/3/2018  43.13  43.06             -0.000232
2  1/4/2018  43.14  43.26              0.004645
3  1/5/2018  43.36  43.75              0.011327

Docs on pct_change.

constantstranger
  • 9,176
  • 2
  • 5
  • 19