Pandas: changing values of a group under a condition

Viewed 71

My df:

import pandas as pd
import numpy as np
df = pd.DataFrame({'id':[1,1,1,2,2],
               'time':['2020-01-01 12:00:15','2020-01-01 12:00:30','2020-01-01 12:00:45','2020-01-03 08:00:00','2020-01-03 08:00:15'],
               'time1':['2020-01-01 12:00:00','2020-01-01 12:00:00','2020-01-01 12:00:00','2020-01-01 12:00:00','2020-01-01 12:00:00'],
               'numb':[1,5,8,0,4]})

df['time'] = pd.to_datetime(df['time'])
df['time1'] = pd.to_datetime(df['time1'])

df['numb_diff'] = df['numb'] - df['numb'].shift()

Output:

    id                 time               time1 numb    numb_diff
0    1  2020-01-01 12:00:15 2020-01-01 12:00:00    1          NaN
1    1  2020-01-01 12:00:30 2020-01-01 12:00:00    5          4.0
2    1  2020-01-01 12:00:45 2020-01-01 12:00:00    8          3.0
3    2  2020-01-03 08:00:00 2020-01-01 12:00:00    0         -8.0
4    2  2020-01-03 08:00:15 2020-01-01 12:00:00    4          4.0

now i would like to set time1 to the lowest value of time of the group(id) whenever the first entry of this id at the position numb_diff is <0.

Expected Output:

    id                 time               time1 numb    numb_diff
0    1  2020-01-01 12:00:15 2020-01-01 12:00:00    1          NaN
1    1  2020-01-01 12:00:30 2020-01-01 12:00:00    5          4.0
2    1  2020-01-01 12:00:45 2020-01-01 12:00:00    8          3.0
3    2  2020-01-03 08:00:00 2020-01-03 08:00:00    0         -8.0    #Changing time1 to the min of time the group(id = 2)
4    2  2020-01-03 08:00:15 2020-01-03 08:00:00    4          4.0
1 Answers

Lets create a boolean mask representing the condition where num_diff is less than zero then groupby this mask on id and transform using first, finally use boolean indexing with this mask to substitute values in time1:

m = df['numb_diff'].lt(0).groupby(df['id']).transform('first')
df.loc[m, 'time1'] = df.groupby('id')['time'].transform('min')

   id                time               time1  numb  numb_diff
0   1 2020-01-01 12:00:15 2020-01-01 12:00:00     1        NaN
1   1 2020-01-01 12:00:30 2020-01-01 12:00:00     5        4.0
2   1 2020-01-01 12:00:45 2020-01-01 12:00:00     8        3.0
3   2 2020-01-03 08:00:00 2020-01-03 08:00:00     0       -8.0
4   2 2020-01-03 08:00:15 2020-01-03 08:00:00     4        4.0
Related