0

Here I have Open and Close Price of Stocks:

Open        Close
1994.988    1994.988
2020.8496   2006.1270142499998
2050.030029 2017.3700764583332
2041.51001  2039.3920492708332
2062.52002  2057.9604493541665
2055.469971 2058.56656934375
2046.73998  2059.327636895833

Now I have to create a new column 'Percent_Change' that will be calculated as (Close - Open) / Open.

Calculations should be done on previous-day values so first row will be Nan then 0, and so on ...

Per_Change
NaN
0
-0.73%
-1.59%
-0.10%
-0.22%
0.15%
0.62%
phuclv
  • 37,963
  • 15
  • 156
  • 475

2 Answers2

1

You can use pd.Series.shift to shift your results:

df['PctChange'] = ((df['Close'] - df['Open']) / df['Close']).shift()

print(df)

          Open        Close  PctChange
0  1994.988000  1994.988000        NaN
1  2020.849600  2006.127014   0.000000
2  2050.030029  2017.370076  -0.007339
3  2041.510010  2039.392049  -0.016189
4  2062.520020  2057.960449  -0.001039
5  2055.469971  2058.566569  -0.002216
6  2046.739980  2059.327637   0.001504

The last row of your expected result will necessarily be missing, as your dataframe index / length is unchanged.

jpp
  • 159,742
  • 34
  • 281
  • 339
0
def compute_precent(row):
    return(float(row[1]-row[0])/row[0])
df['percentage_change']=df.apply(lambda x:compute_precent(x),axis=1)
mad_
  • 8,121
  • 2
  • 25
  • 40
  • 2
    Not recommended. The main benefit of Pandas / NumPy is vectorised computations. `apply` is just a thinly veiled loop. – jpp Aug 07 '18 at 17:08