'Python dataframe: create column with running formula based on values in one row prior
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)
Solution 1:[1]
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.
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 | constantstranger |

