This is my dataframe:
df = pd.DataFrame({'a': list('xxxxxzzz'), 'b':[0,0,1,0,1,0,1,1], 'c': [100, 101, 105, 110, 120, 125, 100, 150], 'd':[0,0,0,1,1,0,0,0]}) I group them:
groups = df.groupby(['a', 'd']) I want to add another column to df that in each group shows the difference (in percentage) between the last value of c that its b is 0 and the last value that its b is 1.
For example in the first group I want to compare c of row 2 and row 1.
My desired groups looks like this:
('x', 0) a b c d result 0 x 0 100 0 3.96 1 x 0 101 0 3.96 2 x 1 105 0 3.96 ('x', 1) a b c d result 3 x 0 110 1 9.09 4 x 1 120 1 9.09 ('z', 0) a b c d result 5 z 0 125 0 20.0 6 z 1 100 0 20.0 7 z 1 150 0 20.0