how do you calculate the first value of each group from every other value in the group in order to calculate change over time?

You can try this.

temp = df.groupby('z').\
    apply(lambda g: g.y - g.y[0]).\
    reset_index().\
    rename(columns={'y': 'diff'}).\
    drop('z', axis=1)

df.merge(temp, how='inner', left_index=True, right_on='level_1').\
    drop('level_1', axis=1)

Return:

x   y   z   diff
0   2   A   0
5   4   A   2
10  6   A   4
0   1   B   0
5   5   B   4
10  9   B   8

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top