karelkieslich
karelkieslich

Reputation: 33

Python pandas: for each unique value in a column, find a minimum value in another column and subtract it from the value of another column

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

Answers (2)

BENY
BENY

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

Henry Ecker
Henry Ecker

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

Related Questions