Skip to content Skip to sidebar Skip to footer

Take Difference Between Pivot Table Columns In Python

I have a dataframe with a Week , Campaign , Placement and Count column. In order to compare counts per weeks by Campaign and Placement I created a pivot table that works great. How

Solution 1:

Use DataFrame.pct_change with selecting last row by positions and multiple by 100 for percentages:

df['diff'] = df.pct_change(axis=1).iloc[:, -1].mul(100)
print (df)
                         2019-10-27  2019-11-03       diff
Campaign Placement Code                                   
A        111111111           4288.0       615.0 -85.657649
         111111112            243.0        11.0 -95.473251
         111111113            598.0        30.0 -94.983278
         111111114           1041.0       377.0 -63.784822
         111111115           7759.0       161.0 -97.924990
B        111111111           1252.0       241.0 -80.750799
         111111112            643.0       124.0 -80.715397
         111111113            135.0        30.0 -77.777778
         111111114           8753.0      2327.0 -73.414829
         111111115           7242.0       112.0 -98.453466

Post a Comment for "Take Difference Between Pivot Table Columns In Python"