Python 熊猫:在条件下更改组的值

Python 熊猫:在条件下更改组的值,python,python-3.x,pandas,dataframe,pandas-groupby,Python,Python 3.x,Pandas,Dataframe,Pandas Groupby,我的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

我的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()

现在我想将
time1
设置为组的
time
的最低值(
id
)每当此
id
numb_diff
位置的第一个条目为时,让我们创建一个布尔掩码,表示
num_diff
小于零的条件,然后在
id
上按此掩码分组,并使用
first
进行变换,最后,使用此掩码的布尔索引替换
time1
中的值:

    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

    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
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