Reputation: 304
I have this dataset, and I'm trying to find the percentage change from the element each row respective to the element at "9" column.
Functionally, what I am trying to do is graph the percentage change throughout the day of a stock price so I can overlay the average percent change for that specific day.
I have tried to tweak around with panda's pct_change()
function but that's only taking the percentage change from each element to the next, which is counter productive.
Here is an example of the dataframe.
Any help or insight would be greatly appreciated.
Hour 9 10 11 12 13 14 15 16
Date
2016-01-05 20.6475 20.5900 20.4225 20.6275 20.1600 19.6500 19.6250 19.4100
2016-01-06 21.3550 20.8675 20.6100 20.6525 20.8900 21.0125 21.0600 20.5125
2016-01-07 23.0075 22.7975 23.0050 23.5975 24.4675 25.2450 25.1600 24.9575
2016-01-08 22.9125 23.2400 23.8575 23.9475 24.0425 24.4000 25.7950 26.7625
2016-01-11 25.7500 25.9100 25.8800 25.9325 26.7650 26.4025 24.9425 24.2725
Upvotes: 0
Views: 362
Reputation: 120449
You can't use pct_change
here, you have to do manually:
>>> df.div(df.iloc[:, 0], axis=0).mul(100).sub(100)
Hour 9 10 11 12 13 14 15 16
Date
2016-01-05 0.0 -0.278484 -1.089720 -0.096864 -2.361061 -4.831093 -4.952173 -5.993462
2016-01-06 0.0 -2.282838 -3.488644 -3.289628 -2.177476 -1.603840 -1.381410 -3.945212
2016-01-07 0.0 -0.912746 -0.010866 2.564381 6.345757 9.725090 9.355645 8.475497
2016-01-08 0.0 1.429351 4.124386 4.517185 4.931806 6.492089 12.580469 16.803055
2016-01-11 0.0 0.621359 0.504854 0.708738 3.941748 2.533981 -3.135922 -5.737864
Test case:
>>> df1 = pd.DataFrame({'A': [1, 2], 'B': [1.25, 2.25], 'C': [1.5, 1.5]})
>>> df.div(df.iloc[:, 0], axis=0).mul(100).sub(100)
A B C
0 0.0 25.0 50.0
1 0.0 12.5 -25.0
Upvotes: 1