根据另一列值向前填充熊猫

时间:2019-01-10 18:48:20

标签: python-3.x pandas datetime dataframe data-manipulation

更新: 我有一个大熊猫数据框,其中包含admitTime,chargeTime,pat_name,pat_rec,它有大约500万条记录。我试图根据其余列的dischargeTime datetime值来向前填充放电时间pat_name列,然后在此之后中断。

df:

admitTime dischargeTime pat_name pat_rec
2013-12-23 20:20:30 2013-12-23 21:12:00 Alex A4536
2013-12-23 21:00:30 2013-12-23 21:01:00 2013-12-23 21:01:30 2013-12-23 21:02:00 2013-12-23 21:02:30 2013-12-23 21:03:00 2013-12-23 21:03:30 2013-12-23 21:04:00 2013-12-23 21:04:30 2013-12-23 21:05:00 2013-12-23 21:05:30 2013-12-23 21:06:00 2013-12-23 21:06:30 2013-12-23 21:07:00 2013-12-23 21:07:30 2013-12-23 21:08:00 2013-12-23 21:08:30 2013-12-23 21:09:00 2013-12-23 21:09:30 2013-12-23 21:10:00 2013-12-23 21:10:30 2013-12-23 21:11:00 2013-12-23 21:11:30 2013-12-23 21:12:00 2013-12-23 21:12:30 2013-12-23 21:13:00 2013-12-23 21:13:30 2013-12-23 21:14:00 2013-12-21:18:00 Sam A4523 2013-12-23 21:14:30 2013-12-23 21:15:00 2013-12-23 21:15:30 2013-12-23 21:16:00 2013-12-23 21:16:30 2013-12-23 21:17:00 2013-12-23 21:17:30 2013-12-23 21:18:00 2013-12-23 21:18:30 2013-12-23 21:19:00 2013-12-23 21:19:30 2013-12-23 21:20:00

理想情况下,我希望我的df看起来像

datetime discchargeTime pat_name pat_rec
2013-12-23 20:20:30 2013-12-23 21:12:00 Alex A4536
2013-12-23 21:00:30 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:01:00 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:01:30 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:02:00 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:02:30 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:03:00 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:03:30 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:04:00 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:04:30 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:05:00 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:05:30 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:06:00 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:06:30 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:07:00 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:07:30 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:08:00 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:08:30 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:09:00 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:09:30 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:10:00 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:10:30 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:11:00 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:11:30 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:12:00 2013-12-23 21:12:00 Alex A4536 2013-12-23 21:12:30 2013-12-23 21:13:00 2013-12-23 21:13:30 2013-12-23 21:14:00 2013-12-21:18:00 Sam A4523 2013-12-23 21:14:30 2013-12-21:18:00 Sam A4523 2013-12-23 21:15:00 2013-12-21:18:00 Sam A4523 2013-12-23 21:15:30 2013-12-21:18:00 Sam A4523 2013-12-23 21:16:00 2013-12-21:18:00 Sam A4523 2013-12-23 21:16:30 2013-12-21:18:00 Sam A4523 2013-12-23 21:17:00 2013-12-21:18:00 Sam A4523 2013-12-23 21:17:30 2013-12-21:18:00 Sam A4523 2013-12-23 21:18:00 2013-12-21:18:00 Sam A4523 2013-12-23 21:18:30 2013-12-23 21:19:00 2013-12-23 21:19:30 2013-12-23 21:20:00

我尝试了df[column_name].ffill(),但后来意识到这样做不正确。

如果能得到任何建议,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

您可以使用以下内容:

mask = df['admitTime'] > df['dischargeTime'].iloc[0] #masking where admit time is greater than discharge time
pd.concat([df[~mask].ffill(),df[mask]]) #ffill the remaining and concat with mask

    admitTime           dischargeTime      pat_name pat_rec
0   2013-12-23 20:20:30 2013-12-23 21:12:00 Alex    A4536
1   2013-12-23 21:00:30 2013-12-23 21:12:00 Alex    A4536
2   2013-12-23 21:01:00 2013-12-23 21:12:00 Alex    A4536
3   2013-12-23 21:01:30 2013-12-23 21:12:00 Alex    A4536
4   2013-12-23 21:02:00 2013-12-23 21:12:00 Alex    A4536
5   2013-12-23 21:02:30 2013-12-23 21:12:00 Alex    A4536
6   2013-12-23 21:03:00 2013-12-23 21:12:00 Alex    A4536
7   2013-12-23 21:03:30 2013-12-23 21:12:00 Alex    A4536
8   2013-12-23 21:04:00 2013-12-23 21:12:00 Alex    A4536
9   2013-12-23 21:04:30 2013-12-23 21:12:00 Alex    A4536
10  2013-12-23 21:05:00 2013-12-23 21:12:00 Alex    A4536
11  2013-12-23 21:05:30 2013-12-23 21:12:00 Alex    A4536
12  2013-12-23 21:06:00 2013-12-23 21:12:00 Alex    A4536
13  2013-12-23 21:06:30 2013-12-23 21:12:00 Alex    A4536
14  2013-12-23 21:07:00 2013-12-23 21:12:00 Alex    A4536
15  2013-12-23 21:07:30 2013-12-23 21:12:00 Alex    A4536
16  2013-12-23 21:08:00 2013-12-23 21:12:00 Alex    A4536
17  2013-12-23 21:08:30 2013-12-23 21:12:00 Alex    A4536
18  2013-12-23 21:09:00 2013-12-23 21:12:00 Alex    A4536
19  2013-12-23 21:09:30 2013-12-23 21:12:00 Alex    A4536
20  2013-12-23 21:10:00 2013-12-23 21:12:00 Alex    A4536
21  2013-12-23 21:10:30 2013-12-23 21:12:00 Alex    A4536
22  2013-12-23 21:11:00 2013-12-23 21:12:00 Alex    A4536
23  2013-12-23 21:11:30 2013-12-23 21:12:00 Alex    A4536
24  2013-12-23 21:12:00 2013-12-23 21:12:00 Alex    A4536
25  2013-12-23 21:12:30 NaT                 NaN     NaN
26  2013-12-23 21:13:00 NaT                 NaN     NaN
................
................

然后,您可以根据需要用空格替换nan。希望这可以帮助。