Reputation: 155
So I have a dataframe with certain values and I want to create a new dataframe with percentage differences from the old dataframe.
My existing dataframe is something like this:
Years A B C D E
2020 801.566522 769.2986786 830.8725406 830.8725406 840.7192069
2021-2030 786.3122361 759.3832642 826.2453567 826.2453567 838.7446964
2031-2050 787.6490105 759.1486884 827.4489946 827.4489946 839.8352851
2041-2050 775.1016567 745.1193567 817.2389842 817.2389842 831.9880991
The new dataframe should calculate the percentage difference between the 2020 values and the values between subsequent decades, and should look something like this.
Years A B C D E
2021-2030 -0.019030593 -0.012888901 -0.005569066 -0.005569066 -0.002348597
2031-2050 -0.01736289 -0.013193823 -0.004120423 -0.004120423 -0.001051388
2041-2050 -0.03301643 -0.031430344 -0.016408722 -0.016408722 -0.010385284
I can do this in excel quite easily but since I have a large number of these files, I wanted to do it in python instead. Can anyone help me out with a basic solution for this?
Upvotes: 0
Views: 35
Reputation: 846
Just do
df=yourdataframe.pct_change()
The first row of the dataframe will be of NaN values and if you'd like to keep it in the format you'd shown just
yourdataframe=df.iloc[1:]
This will remove the first row of the dataframe
Upvotes: 1