Pandas - Adjust stock prices to stock splits

Viewed 1357

I have a DataFrame, with prices data and stock splits data. I want to put all the prices on the same page, so for example if we had a stock split of 0.11111 (1/9), from now on all the stock prices would be multiplied by 9.

So for example if this is my initial dataframe:

df=         Price  Stock_Splits
         0   100       0 
         1   99        0
         2   10        0.1111111
         3   8         0
         4   8.5       0
         5   4         0.5

The "Price" column will become:

 df=         Price  Stock_Splits
         0   100       0 
         1   99        0
         2   90        0.1111111
         3   72         0
         4   76.5       0
         5   72         0.5
1 Answers

Here is one example:

df['New_Price'] = (1 / df.Stock_Splits).replace(np.inf, 1).cumprod() * df.Price

   Price  Stock_Splits   New_Price
0  100.0      0.000000  100.000000
1   99.0      0.000000   99.000000
2   10.0      0.111111   90.000009
3    8.0      0.000000   72.000007
4    8.5      0.000000   76.500008
5    4.0      0.500000   72.000007
Related