Python:按规则对数据进行分组并保存列值



我正在尝试在分组后使用特定逻辑保存列值。这是我的数据帧";user_payments":

id  createdAt   updatedAt   rentEndAt   rentComplete    status  price   value   debt    attemptsCount   receipt creditCardId    tariffId    rentId
1468    1402    2020-10-02 12:52:03.895405  2020-10-02 19:01:30.917097  2020-10-03 12:52:00 False   succeeded   100.0   100.0   0.0 0   {'id': '27093630-000f-5000-a000-128a5fce2230',...   505 2   637.0
1466    1332    2020-10-01 12:52:04.184750  2020-10-02 12:52:03.915606  2020-10-02 12:52:00 True    succeeded   100.0   100.0   0.0 0   {'id': '2707e4b0-000f-5000-9000-1f9c30bf3898',...   505 2   637.0
1373    1258    2020-09-30 12:52:04.060209  2020-10-01 12:52:04.214163  2020-10-01 12:52:00 True    succeeded   100.0   100.0   0.0 0   {'id': '27069330-000f-5000-a000-1c98d8f9e249',...   505 2   637.0
1318    1212    2020-09-29 12:52:04.018585  2020-09-30 12:52:04.085714  2020-09-30 12:52:00 True    succeeded   100.0   100.0   0.0 0   {'id': '270541b0-000f-5000-8000-1bf091d27028',...   505 2   635.0
1254    1166    2020-09-28 12:52:04.002709  2020-09-29 12:52:04.047428  2020-09-29 12:52:00 True    succeeded   100.0   100.0   0.0 0   {'id': '2703f030-000f-5000-9000-1d3770fbe41a',...   505 2   635.0

我正试图用租用时间间隔(即"createdAt"one_answers"updatedAt"列之间的差异(取回一个表,但我需要保存一个列";状态":带有聚合规则的['True','False']:if for specific"rentId";存在";"错误";在列";状态";对于任何一行;"错误";价值如果不存在";错误"-值";真"必须保存。

这是我的分组公式:

time_to_rent = user_payments.groupby(['rentId','creditCardId']).agg({'createdAt': np.min, 'updatedAt': np.max})
time_to_rent['rent_time'] = time_to_rent['updatedAt'] - time_to_rent['createdAt']

在返回的结果中,最好有:

createdAt   updatedAt   rent_time
rentId  creditCardId    status              
637.0   505 2020-09-27 08:44:13.431341  2020-09-27 09:13:45.675674  0 days 00:29:32.244333  False
635.0   505 2020-09-27 09:14:27.188478  2020-09-27 12:51:03.394003  0 days 03:36:36.205525  True

你能告诉我怎么加";"状态";柱

它可以通过使用lambda函数来实现:

user_payments["status"] = user_payments["rentId"].apply(lambda x: False if False in list(user_payments[user_payments["rentId"] == x]["status"]) else True)

这将检查是否有任何具有相同rentId状态的行为False,否则将其设置为False。

最新更新