I have a dataset with several repeated measures over a week for each participant, which I aggregated using `groupby` and `.agg` functions to have the first and the last measurement taken as follows:
df2=df.groupby("ID").agg(['first', 'last']).stack()
Now it looks like this:
| ID | | Age | Score | Measure |
| -- | ---- | --- | ----- | ------ |
| 0 | first| 69 | 48 | 34 |
| | last | 69 | 52 | 33 |
| 1 | first| 58 | 28 | 89 |
| | last | 58 | 14 | 34 |
I want to create a dataset using first and last value aggregates to create a difference between these values for Score and Measure variables. It should look like this:
| ID | Age | Score_diff | Measure_diff |
| -- | --- | ---------- | ------------ |
| 0 | 69 | 4 | 1 |
| 1 | 58 | 14 | 55 |
I am not sure how to approach this (I'm a Python newbie). Is there anyone who could help me out?
Best wishes