Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python (Pandas) calculate percent change

Tags:

python

pandas

I have the following dataset for numerous stocks, and am using the following formula to calculate the percent change df7['Change']=(df7.Close.pct_change())*100

However, I would like how I can modify this formula, or write a new one that will make the change as NaN the first time the stock appears in the data frame.

So in the example below the change for AAPL the first time it appears in data doesnt say -83%

      Date        Open        High         Low       Close    Volume  \
 2015-11-02  711.059998  721.619995  705.849976  721.109985   1871100   
 2015-11-03  718.859985  724.650024  714.719971  722.159973   1560800   
 2015-11-04  722.000000  733.099976  721.900024  728.109985   1704600   
 2015-11-05  729.469971  739.479980  729.469971  731.250000   1860400   
 2015-11-06  731.500000  735.409973  727.010010  733.760010   1509600   
 2015-11-09  730.200012  734.710022  719.429993  724.890015   2065600   
 2015-11-10  724.400024  730.590027  718.500000  728.320007   1603900   
 2015-11-02  120.800003  121.360001  119.610001  121.180000  32203300   
 2015-11-03  120.790001  123.489998  120.699997  122.570000  45519000   
 2015-11-04  123.129997  123.820000  121.620003  122.000000  44886100   
 2015-11-05  121.849998  122.690002  120.180000  120.919998  39552700   
 2015-11-06  121.110001  121.809998  120.620003  121.059998  33042300   
 2015-11-09  120.959999  121.809998  120.050003  120.570000  33871400   
 2015-11-10  116.900002  118.070000  116.059998  116.769997  59127900   

   Adj Close  Symbol    Change  
  721.109985   GOOG        NaN  
  722.159973   GOOG   0.145607  
  728.109985   GOOG   0.823919  
  731.250000   GOOG   0.431256  
  733.760010   GOOG   0.343249  
  724.890015   GOOG  -1.208841  
  728.320007   GOOG   0.473174  
  120.663499   AAPL -83.361709  
  122.047573   AAPL   1.147054  
  121.480003   AAPL  -0.465040  
  120.919998   AAPL  -0.885248  
  121.059998   AAPL   0.115779  
  120.570000   AAPL  -0.404756  
  116.769997   AAPL  -3.151699 

I have used the following code to generate this information:

import pandas as pd
from pandas.io.data import DataReader
from datetime import datetime

df7 = pd.DataFrame()
symbols = ['GOOG','AAPL']

for symbol in symbols:
    ClosingPrice = DataReader(symbol, 'yahoo', datetime(2015,11,1), datetime(2015,11,10))
    ClosingPrice = ClosingPrice.reset_index()
    ClosingPrice['Symbol'] = symbol
    df7 = df7.append(ClosingPrice)

Any help will be appreciated. Thank you!

like image 663
Raptor776 Avatar asked Sep 26 '22 02:09

Raptor776


1 Answers

You could use groupby method:

df['Change'] = df.groupby('Symbol').Close.pct_change()

In [20]: df
Out[20]:
                  Open        High         Low       Close    Volume  \
Date
2015-11-02  711.059998  721.619995  705.849976  721.109985   1871100
2015-11-03  718.859985  724.650024  714.719971  722.159973   1560800
2015-11-04  722.000000  733.099976  721.900024  728.109985   1704600
2015-11-05  729.469971  739.479980  729.469971  731.250000   1860400
2015-11-06  731.500000  735.409973  727.010010  733.760010   1509600
2015-11-09  730.200012  734.710022  719.429993  724.890015   2065600
2015-11-10  724.400024  730.590027  718.500000  728.320007   1603900
2015-11-02  120.800003  121.360001  119.610001  121.180000  32203300
2015-11-03  120.790001  123.489998  120.699997  122.570000  45519000
2015-11-04  123.129997  123.820000  121.620003  122.000000  44886100
2015-11-05  121.849998  122.690002  120.180000  120.919998  39552700
2015-11-06  121.110001  121.809998  120.620003  121.059998  33042300
2015-11-09  120.959999  121.809998  120.050003  120.570000  33871400
2015-11-10  116.900002  118.070000  116.059998  116.769997  59127900

             Adj_Close Symbol    Change
Date
2015-11-02  721.109985   GOOG       NaN
2015-11-03  722.159973   GOOG  0.001456
2015-11-04  728.109985   GOOG  0.008239
2015-11-05  731.250000   GOOG  0.004313
2015-11-06  733.760010   GOOG  0.003432
2015-11-09  724.890015   GOOG -0.012088
2015-11-10  728.320007   GOOG  0.004732
2015-11-02  120.663499   AAPL       NaN
2015-11-03  122.047573   AAPL  0.011471
2015-11-04  121.480003   AAPL -0.004650
2015-11-05  120.919998   AAPL -0.008852
2015-11-06  121.059998   AAPL  0.001158
2015-11-09  120.570000   AAPL -0.004048
2015-11-10  116.769997   AAPL -0.031517
like image 187
Anton Protopopov Avatar answered Oct 13 '22 00:10

Anton Protopopov