Michelle
Michelle

Reputation: 263

Replace all values of a column by first value - grouped by ID

my pandas dataframe looks like this:

ID    trackintime               trackouttime            time difference
54    2020-01-01 00:00:03       2020-01-01 00:00:05     00:00:02
55    2020-01-01 00:00:04       2020-01-01 00:00:06     00:00:02
54    2020-01-01 00:00:05       2020-01-01 00:00:10     00:00:05
55    2020-01-01 00:00:06       2020-01-01 00:00:10     00:00:04

I would like to replace the value of the time difference for every ID entry by the value of the time difference of the first entry (timewise) per ID.

The result should look like this:

ID    trackintime               trackouttime            time difference
54    2020-01-01 00:00:03       2020-01-01 00:00:05     00:00:02
55    2020-01-01 00:00:04       2020-01-01 00:00:06     00:00:02
54    2020-01-01 00:00:05       2020-01-01 00:00:10     00:00:02
55    2020-01-01 00:00:06       2020-01-01 00:00:08     00:00:02

Thanks in advance for any help!

Upvotes: 1

Views: 302

Answers (1)

jezrael
jezrael

Reputation: 863791

Use GroupBy.transform with GroupBy.first and subtract values:

g = df.groupby('ID')
df['time difference'] = (g['trackouttime'].transform('first')
                          .sub(g['trackintime'].transform('first')))

Upvotes: 3

Related Questions