我现在处于困境中。我想在下面的数据集中的新字段中添加baseline_value和critical_value。
Baseline_value是“ result_value”,它是“ start_date”和“ collection_date”之间的比较。在“ id”分组中,“ collection_date”必须早于“ start_date”,并且最接近。
critical_value是“ result_value”,也是“ start_date”和“ collection_date”之间的比较。但是“ collection_date”必须在48小时内大于“ start_date”,并在“ id”分组中选择“ result_value”的最大值。
我设法以某种方式获得了基线值。但不是critical_value。我尝试在任何地方搜索,最后以发布问题寻求专家的帮助。
基线值代码:
df=pd.read_excel(loc,'sheet1')
def before(s, df):
grp = df.loc[s.index]
idx = grp['collection_date'].searchsorted(grp['start_date'])
result = grp['result_value'].iloc[idx-1]
return result
df = df.sort_values(by=['collection_date'])
grouped = df.groupby('id')
df["baseline_value"] = grouped['collection_date'].transform(before, df)
请在下面找到raw_data。
id collection_date result_value start_date
1 30/08/2017 11:42 66 05/05/2017 14:55
1 28/06/2017 7:07 46 05/05/2017 14:55
1 28/04/2017 9:23 75 05/05/2017 14:55
1 27/06/2017 6:28 56 05/05/2017 14:55
1 26/05/2017 5:07 54 05/05/2017 14:55
1 25/06/2017 6:48 48 05/05/2017 14:55
1 24/07/2017 5:18 58 05/05/2017 14:55
1 24/05/2017 5:51 48 05/05/2017 14:55
1 23/06/2017 6:03 54 05/05/2017 14:55
1 21/06/2017 5:35 48 05/05/2017 14:55
1 21/05/2017 21:27 70 05/05/2017 14:55
1 19/06/2017 5:27 45 05/05/2017 14:55
1 17/07/2017 5:18 51 05/05/2017 14:55
1 16/06/2017 6:42 45 05/05/2017 14:55
1 14/06/2017 6:40 46 05/05/2017 14:55
1 12/06/2017 11:48 57 05/05/2017 14:55
1 11/04/2018 10:07 65 05/05/2017 14:55
1 10/08/2017 11:35 61 05/05/2017 14:55
1 10/07/2017 4:39 47 05/05/2017 14:55
1 08/06/2017 21:35 54 05/05/2017 14:55
1 08/05/2017 4:56 45 05/05/2017 14:55
1 07/05/2017 4:56 61 05/05/2017 14:55
1 06/05/2017 4:54 62 05/05/2017 14:55
1 03/08/2017 12:48 99 05/05/2017 14:55
1 03/07/2017 5:05 40 05/05/2017 14:55
2 23/03/2017 9:22 75 31/03/2017 9:55
2 01/04/2017 5:32 78 31/03/2017 9:55
3 23/10/2017 11:17 73 04/04/2017 10:15
3 22/06/2017 8:33 71 04/04/2017 10:15
3 20/07/2017 11:41 70 04/04/2017 10:15
3 16/03/2017 14:42 72 04/04/2017 10:15
3 08/04/2017 14:04 57 04/04/2017 10:15
3 07/09/2017 9:51 84 04/04/2017 10:15
3 05/04/2018 9:21 74 04/04/2017 10:15
3 05/04/2017 5:09 73 04/04/2017 10:15
4 28/12/2017 5:21 232 15/03/2017 9:15
4 27/12/2017 5:26 231 15/03/2017 9:15
4 27/12/2017 13:27 249 15/03/2017 9:15
4 26/12/2017 20:52 247 15/03/2017 9:15
4 22/03/2017 4:38 277 15/03/2017 9:15
4 21/03/2017 4:52 333 15/03/2017 9:15
4 21/02/2017 14:39 208 15/03/2017 9:15
4 20/03/2017 5:07 418 15/03/2017 9:15
4 19/03/2017 5:14 456 15/03/2017 9:15
4 19/03/2017 16:08 452 15/03/2017 9:15
4 18/03/2017 5:16 422 15/03/2017 9:15
4 18/03/2017 16:23 453 15/03/2017 9:15
4 17/03/2017 21:03 397 15/03/2017 9:15
4 17/03/2017 5:12 317 15/03/2017 9:15
4 17/03/2017 14:17 361 15/03/2017 9:15
4 16/03/2017 21:22 307 15/03/2017 9:15
4 16/03/2017 17:31 289 15/03/2017 9:15
4 16/03/2017 5:10 228 15/03/2017 9:15
4 15/03/2017 19:19 214 15/03/2017 9:15
4 14/03/2017 15:55 206 15/03/2017 9:15
4 12/09/2017 9:04 210 15/03/2017 9:15
4 08/03/2017 9:05 211 15/03/2017 9:15
4 06/03/2018 9:27 228 15/03/2017 9:15
4 02/06/2017 12:00 209 15/03/2017 9:15
4 02/01/2018 11:32 208 15/03/2017 9:15
4 01/03/2018 11:17 259 15/03/2017 9:15
5 22/02/2017 9:10 63 17/03/2017 10:45
5 21/03/2017 4:54 47 17/03/2017 10:45
5 19/03/2017 5:18 64 17/03/2017 10:45
5 18/03/2017 4:58 63 17/03/2017 10:45
5 16/03/2017 15:03 57 17/03/2017 10:45