Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I calculate cumulative percentage change from beginning period

I am trying to create a DataFrame with a rolling cumulative percentage change. I would like to show the percentage change of the stock from the initial buy date (2014-09-05).

import pandas as pd
import pandas.io.data as web

cvs = web.get_data_yahoo('cvs', '2014-09-05')['Adj Close']

cvsChange = cvs[1:] / cvs.shift(1) - 1
like image 632
Brian Avatar asked Feb 09 '23 08:02

Brian


2 Answers

Thank you @EdChum

What I was looking for was...

PriceChange = cvs.diff().cumsum()
PercentageChange = PriceChange / cvs.iloc[0]
like image 75
Brian Avatar answered Feb 12 '23 01:02

Brian


How about this:

(cvs.iloc[-1] - cvs.iloc[0]) / cvs.iloc[0] * 100
like image 31
Brent Washburne Avatar answered Feb 11 '23 23:02

Brent Washburne