Pandas:在某种条件下改变群体的价值

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

输出:


    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

现在我想设置为组( )time1的最低值,只要该位置的第一个条目<0。timeididnumb_diff


预期输出:


    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


ABOUTYOU
浏览 118回答 1
1回答

慕姐4208626

让我们创建一个表示条件的布尔掩码,其中num_diff小于零,然后对该掩码进行分组id并使用 进行转换first,最后使用此掩码的布尔索引来替换 中的值time1:m = df['numb_diff'].lt(0).groupby(df['id']).transform('first')df.loc[m, 'time1'] = df.groupby('id')['time'].transform('min')&nbsp; &nbsp;id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;time1&nbsp; numb&nbsp; numb_diff0&nbsp; &nbsp;1 2020-01-01 12:00:15 2020-01-01 12:00:00&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp; &nbsp; NaN1&nbsp; &nbsp;1 2020-01-01 12:00:30 2020-01-01 12:00:00&nbsp; &nbsp; &nbsp;5&nbsp; &nbsp; &nbsp; &nbsp; 4.02&nbsp; &nbsp;1 2020-01-01 12:00:45 2020-01-01 12:00:00&nbsp; &nbsp; &nbsp;8&nbsp; &nbsp; &nbsp; &nbsp; 3.03&nbsp; &nbsp;2 2020-01-03 08:00:00 2020-01-03 08:00:00&nbsp; &nbsp; &nbsp;0&nbsp; &nbsp; &nbsp; &nbsp;-8.04&nbsp; &nbsp;2 2020-01-03 08:00:15 2020-01-03 08:00:00&nbsp; &nbsp; &nbsp;4&nbsp; &nbsp; &nbsp; &nbsp; 4.0
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python