Reputation: 33
I have a df like this:
df = pd.DataFrame({'Person': ['Alex', 'Alex', 'Alex', 'Eva', 'Eva', 'Eva'],
'Time': [30, 40, 60, 35, 55, 57]})
I want to create a column which will say how much the time differs from the person's lowest time:
df = pd.DataFrame({'Person': ['Alex', 'Alex', 'Alex', 'Eva', 'Eva', 'Eva'],
'Time': [30, 40, 60, 35, 55, 57],
'Difference': [0, 10, 30, 0, 20, 22})
I have tried groupby() but I must be doing it wrong. What is the best way to do this? Many thanks!
Upvotes: 1
Views: 802
Reputation: 323226
Sometime apply
is more easy to understand
df.groupby('Person')['Difference'].apply(lambda x : x-x.min())
Out[236]:
0 0
1 10
2 30
3 0
4 20
5 22
Name: Difference, dtype: int64
Upvotes: 0
Reputation: 35626
Let's try Time
minus groupby transform
'min' to get difference between the current time value and the minimum time value per group:
df['Difference'] = df['Time'] - df.groupby('Person')['Time'].transform('min')
df
:
Person Time Difference
0 Alex 30 0
1 Alex 40 10
2 Alex 60 30
3 Eva 35 0
4 Eva 55 20
5 Eva 57 22
Upvotes: 1