My Dataframe has the following structure:
patient_id | timestamp | measurement
A | 2014-10-10 | 5.7
A | 2014-10-11 | 6.3
B | 2014-10-11 | 6.1
B | 2014-10-10 | 4.1
I would like to calculate a delta
(difference) between each measurement of each patient.
The result should look like:
patient_id | timestamp | measurement | delta
A | 2014-10-10 | 5.7 | NaN
A | 2014-10-11 | 6.3 | 0.6
B | 2014-10-11 | 6.1 | 2.0
B | 2014-10-10 | 4.1 | NaN
How can this be done most-elegantly in pandas ?
Call
transform
on the 'measurement' column and pass the methoddiff
, transform returns a series with an index aligned to the original df:EDIT
If you are intending to apply some sorting on the result of
transform
then sort the df first: