我对DataFrames有所关注,并且我正在使用merge合并到列,如下所示:
df1包含登录/注销时间
+-------+------------+-------------------------------+-------------------------------+
| P_Id | Date | LoginTime | LogoutTime |
+-------+------------+-------------------------------+-------------------------------+
| 10830 | 2019-11-13 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 10830 | 2019-11-14 | 2019-11-14 22:11:02.429253600 | 2019-11-15 03:24:03.297526800 |
| 10830 | 2019-11-15 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 10830 | 2019-11-16 | 2019-11-16 17:49:44.048717800 | 2019-11-17 01:47:12.073102200 |
| 10830 | 2019-11-17 | 2019-11-17 14:18:20.549867100 | 2019-11-18 01:38:23.505849200 |
+-------+------------+-------------------------------+-------------------------------+
df2包含一次登录和注销之间发生的事件的时间。
+------+-------------------------------+-------+-------------------------------+-------+------------+-----+----------+------+---------------------------+
| Id | Start | P_Id | End | Month | Date | Day | Week_day | Hour | After |
+------+-------------------------------+-------+-------------------------------+-------+------------+-----+----------+------+---------------------------+
| 5190 | 2019-11-13 16:00:46.355864100 | 10830 | 2019-11-13 16:01:52.214816300 | 11 | 2019-11-13 | 13 | 2 | 16 | |
| 5191 | 2019-11-13 16:02:24.726404400 | 10830 | 2019-11-13 16:03:02.068703500 | 11 | 2019-11-13 | 13 | 2 | 16 | 0 days 00:00:32.511588100 |
| 5193 | 2019-11-13 16:03:57.336799800 | 10830 | 2019-11-13 16:06:52.735558300 | 11 | 2019-11-13 | 13 | 2 | 16 | 0 days 00:00:55.268096300 |
| 5194 | 2019-11-13 16:08:50.803699400 | 10830 | 2019-11-13 16:11:31.797923600 | 11 | 2019-11-13 | 13 | 2 | 16 | 0 days 00:01:58.068141100 |
| 5198 | 2019-11-13 16:51:03.613695300 | 10830 | 2019-11-13 17:28:38.597633100 | 11 | 2019-11-13 | 13 | 2 | 17 | 0 days 00:39:31.815771700 |
| 5200 | 2019-11-13 17:30:35.305265600 | 10830 | 2019-11-13 18:09:20.454238200 | 11 | 2019-11-13 | 13 | 2 | 18 | 0 days 00:01:56.707632500 |
| 5211 | 2019-11-13 22:14:37.616688600 | 10830 | 2019-11-13 22:17:27.129620500 | 11 | 2019-11-13 | 13 | 2 | 22 | 0 days 04:05:17.162450400 |
| 5214 | 2019-11-13 22:44:01.224241000 | 10830 | 2019-11-13 22:59:39.863998100 | 11 | 2019-11-13 | 13 | 2 | 22 | 0 days 00:26:34.094620500 |
| 5256 | 2019-11-14 23:21:33.186943200 | 10830 | 2019-11-14 23:56:16.082169100 | 11 | 2019-11-14 | 14 | 3 | 23 | |
| 5258 | 2019-11-15 00:24:57.483800900 | 10830 | 2019-11-15 00:31:47.959459400 | 11 | 2019-11-15 | 15 | 4 | 0 | |
| 5259 | 2019-11-15 00:33:50.642483100 | 10830 | 2019-11-15 00:49:09.138852000 | 11 | 2019-11-15 | 15 | 4 | 0 | 0 days 00:02:02.683023700 |
| 5261 | 2019-11-15 01:44:44.293864600 | 10830 | 2019-11-15 01:59:07.159142200 | 11 | 2019-11-15 | 15 | 4 | 1 | 0 days 00:55:35.155012600 |
| 5263 | 2019-11-15 02:05:33.623635000 | 10830 | 2019-11-15 02:12:15.581544000 | 11 | 2019-11-15 | 15 | 4 | 2 | 0 days 00:06:26.464492800 |
| 5286 | 2019-11-15 17:54:30.140008300 | 10830 | 2019-11-15 18:53:40.433555800 | 11 | 2019-11-15 | 15 | 4 | 18 | 0 days 15:42:14.558464300 |
| 5296 | 2019-11-15 20:57:29.508330200 | 10830 | 2019-11-15 21:02:44.851496700 | 11 | 2019-11-15 | 15 | 4 | 21 | 0 days 02:03:49.074774400 |
| 5297 | 2019-11-15 21:22:24.220020000 | 10830 | 2019-11-15 21:35:19.343132000 | 11 | 2019-11-15 | 15 | 4 | 21 | 0 days 00:19:39.368523300 |
| 5300 | 2019-11-15 21:57:41.880812100 | 10830 | 2019-11-15 22:15:32.520381000 | 11 | 2019-11-15 | 15 | 4 | 22 | 0 days 00:22:22.537680100 |
| 5301 | 2019-11-15 23:00:03.505284200 | 10830 | 2019-11-15 23:15:31.295551700 | 11 | 2019-11-15 | 15 | 4 | 23 | 0 days 00:44:30.984903200 |
| 5302 | 2019-11-15 23:17:00.816945700 | 10830 | 2019-11-15 23:29:21.781482300 | 11 | 2019-11-15 | 15 | 4 | 23 | 0 days 00:01:29.521394000 |
| 5303 | 2019-11-15 23:45:16.858411100 | 10830 | 2019-11-16 00:09:29.816557900 | 11 | 2019-11-16 | 16 | 5 | 0 | |
| | 2019-11-16 00:27:19.138782200 | 10830 | 2019-11-16 00:42:06.387264200 | 11 | 2019-11-16 | 16 | 5 | 0 | 0 days 00:17:49.322224300 |
| | 2019-11-16 00:57:33.712924800 | 10830 | 2019-11-16 00:58:06.315696400 | 11 | 2019-11-16 | 16 | 5 | 0 | 0 days 00:15:27.325660600 |
| | 2019-11-16 01:09:55.180647700 | 10830 | 2019-11-16 01:15:08.916555400 | 11 | 2019-11-16 | 16 | 5 | 1 | 0 days 00:11:48.864951300 |
| | 2019-11-16 18:04:30.656633300 | 10830 | 2019-11-16 19:12:32.382217300 | 11 | 2019-11-16 | 16 | 5 | 19 | 0 days 16:49:21.740077900 |
+------+-------------------------------+-------+-------------------------------+-------+------------+-----+----------+------+---------------------------+
出于时间差计算的目的,我想将它们合并(在一次登录和注销中计算事件之间的时间差),以便可以将注销和登录时间添加为df2旁边的列,以便我使用
m=pd.merge(gf, df, how ='inner', on=['DriverId', 'Month', 'Date']) // calculated Month and Date for df1 as well
这给了我以下输出:
+------+------+-------------------------------+----------+-------------------------------+-------+------------+-----+----------+------+---------------------------+-------------------------------+-------------------------------+
| Id | P_Id | TripStartDate | DriverId | TripEndDate | Month | Date | Day | Week_day | Hour | f | LoginTime | LogoutTime |
+------+------+-------------------------------+----------+-------------------------------+-------+------------+-----+----------+------+---------------------------+-------------------------------+-------------------------------+
| 5190 | | 2019-11-13 16:00:46.355864100 | 10830 | 2019-11-13 16:01:52.214816300 | 11 | 2019-11-13 | 13 | 2 | 16 | | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5191 | | 2019-11-13 16:02:24.726404400 | 10830 | 2019-11-13 16:03:02.068703500 | 11 | 2019-11-13 | 13 | 2 | 16 | 0 days 00:00:32.511588100 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5193 | | 2019-11-13 16:03:57.336799800 | 10830 | 2019-11-13 16:06:52.735558300 | 11 | 2019-11-13 | 13 | 2 | 16 | 0 days 00:00:55.268096300 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5194 | 2489 | 2019-11-13 16:08:50.803699400 | 10830 | 2019-11-13 16:11:31.797923600 | 11 | 2019-11-13 | 13 | 2 | 16 | 0 days 00:01:58.068141100 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5198 | 2492 | 2019-11-13 16:51:03.613695300 | 10830 | 2019-11-13 17:28:38.597633100 | 11 | 2019-11-13 | 13 | 2 | 17 | 0 days 00:39:31.815771700 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5200 | 2494 | 2019-11-13 17:30:35.305265600 | 10830 | 2019-11-13 18:09:20.454238200 | 11 | 2019-11-13 | 13 | 2 | 18 | 0 days 00:01:56.707632500 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5211 | 2504 | 2019-11-13 22:14:37.616688600 | 10830 | 2019-11-13 22:17:27.129620500 | 11 | 2019-11-13 | 13 | 2 | 22 | 0 days 04:05:17.162450400 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5214 | | 2019-11-13 22:44:01.224241000 | 10830 | 2019-11-13 22:59:39.863998100 | 11 | 2019-11-13 | 13 | 2 | 22 | 0 days 00:26:34.094620500 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5256 | | 2019-11-14 23:21:33.186943200 | 10830 | 2019-11-14 23:56:16.082169100 | 11 | 2019-11-14 | 14 | 3 | 23 | | 2019-11-14 22:11:02.429253600 | 2019-11-15 03:24:03.297526800 |
| 5258 | | 2019-11-15 00:24:57.483800900 | 10830 | 2019-11-15 00:31:47.959459400 | 11 | 2019-11-15 | 15 | 4 | 0 | | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5259 | | 2019-11-15 00:33:50.642483100 | 10830 | 2019-11-15 00:49:09.138852000 | 11 | 2019-11-15 | 15 | 4 | 0 | 0 days 00:02:02.683023700 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5261 | | 2019-11-15 01:44:44.293864600 | 10830 | 2019-11-15 01:59:07.159142200 | 11 | 2019-11-15 | 15 | 4 | 1 | 0 days 00:55:35.155012600 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5263 | | 2019-11-15 02:05:33.623635000 | 10830 | 2019-11-15 02:12:15.581544000 | 11 | 2019-11-15 | 15 | 4 | 2 | 0 days 00:06:26.464492800 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5286 | 2543 | 2019-11-15 17:54:30.140008300 | 10830 | 2019-11-15 18:53:40.433555800 | 11 | 2019-11-15 | 15 | 4 | 18 | 0 days 15:42:14.558464300 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5296 | | 2019-11-15 20:57:29.508330200 | 10830 | 2019-11-15 21:02:44.851496700 | 11 | 2019-11-15 | 15 | 4 | 21 | 0 days 02:03:49.074774400 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5297 | | 2019-11-15 21:22:24.220020000 | 10830 | 2019-11-15 21:35:19.343132000 | 11 | 2019-11-15 | 15 | 4 | 21 | 0 days 00:19:39.368523300 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5300 | | 2019-11-15 21:57:41.880812100 | 10830 | 2019-11-15 22:15:32.520381000 | 11 | 2019-11-15 | 15 | 4 | 22 | 0 days 00:22:22.537680100 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5301 | | 2019-11-15 23:00:03.505284200 | 10830 | 2019-11-15 23:15:31.295551700 | 11 | 2019-11-15 | 15 | 4 | 23 | 0 days 00:44:30.984903200 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5302 | | 2019-11-15 23:17:00.816945700 | 10830 | 2019-11-15 23:29:21.781482300 | 11 | 2019-11-15 | 15 | 4 | 23 | 0 days 00:01:29.521394000 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5303 | | 2019-11-15 23:45:16.858411100 | 10830 | 2019-11-16 00:09:29.816557900 | 11 | 2019-11-16 | 16 | 5 | 0 | | 2019-11-16 17:49:44.048717800 | 2019-11-17 01:47:12.073102200 |
| 5304 | | 2019-11-16 00:27:19.138782200 | 10830 | 2019-11-16 00:42:06.387264200 | 11 | 2019-11-16 | 16 | 5 | 0 | 0 days 00:17:49.322224300 | 2019-11-16 17:49:44.048717800 | 2019-11-17 01:47:12.073102200 |
| 5305 | | 2019-11-16 00:57:33.712924800 | 10830 | 2019-11-16 00:58:06.315696400 | 11 | 2019-11-16 | 16 | 5 | 0 | 0 days 00:15:27.325660600 | 2019-11-16 17:49:44.048717800 | 2019-11-17 01:47:12.073102200 |
| 5306 | | 2019-11-16 01:09:55.180647700 | 10830 | 2019-11-16 01:15:08.916555400 | 11 | 2019-11-16 | 16 | 5 | 1 | 0 days 00:11:48.864951300 | 2019-11-16 17:49:44.048717800 | 2019-11-17 01:47:12.073102200 |
| 5329 | | 2019-11-16 18:04:30.656633300 | 10830 | 2019-11-16 19:12:32.382217300 | 11 | 2019-11-16 | 16 | 5 | 19 | 0 days 16:49:21.740077900 | 2019-11-16 17:49:44.048717800 | 2019-11-17 01:47:12.073102200 |
+------+------+-------------------------------+----------+-------------------------------+-------+------------+-----+----------+------+---------------------------+-------------------------------+-------------------------------+
但是问题在于,对于某些登录和注销date
进行了更改,并且下次登录也可能与上次注销的日期相同,我正在寻找类似以下内容的东西。
+------+-------------------------------+-------+-------------------------------+-------+------------+-----+----------+------+---------------------------+-------------------------------+-------------------------------+
| Id | Start | P_Id | End | Month | Date | Day | Week_day | Hour | After | LoginTime | LogoutTime |
+------+-------------------------------+-------+-------------------------------+-------+------------+-----+----------+------+---------------------------+-------------------------------+-------------------------------+
| 5190 | 2019-11-13 16:00:46.355864100 | 10830 | 2019-11-13 16:01:52.214816300 | 11 | 2019-11-13 | 13 | 2 | 16 | | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5191 | 2019-11-13 16:02:24.726404400 | 10830 | 2019-11-13 16:03:02.068703500 | 11 | 2019-11-13 | 13 | 2 | 16 | 0 days 00:00:32.511588100 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5193 | 2019-11-13 16:03:57.336799800 | 10830 | 2019-11-13 16:06:52.735558300 | 11 | 2019-11-13 | 13 | 2 | 16 | 0 days 00:00:55.268096300 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5194 | 2019-11-13 16:08:50.803699400 | 10830 | 2019-11-13 16:11:31.797923600 | 11 | 2019-11-13 | 13 | 2 | 16 | 0 days 00:01:58.068141100 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5198 | 2019-11-13 16:51:03.613695300 | 10830 | 2019-11-13 17:28:38.597633100 | 11 | 2019-11-13 | 13 | 2 | 17 | 0 days 00:39:31.815771700 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5200 | 2019-11-13 17:30:35.305265600 | 10830 | 2019-11-13 18:09:20.454238200 | 11 | 2019-11-13 | 13 | 2 | 18 | 0 days 00:01:56.707632500 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5211 | 2019-11-13 22:14:37.616688600 | 10830 | 2019-11-13 22:17:27.129620500 | 11 | 2019-11-13 | 13 | 2 | 22 | 0 days 04:05:17.162450400 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5214 | 2019-11-13 22:44:01.224241000 | 10830 | 2019-11-13 22:59:39.863998100 | 11 | 2019-11-13 | 13 | 2 | 22 | 0 days 00:26:34.094620500 | 2019-11-13 15:58:44.977126500 | 2019-11-14 00:13:19.111769600 |
| 5256 | 2019-11-14 23:21:33.186943200 | 10830 | 2019-11-14 23:56:16.082169100 | 11 | 2019-11-14 | 14 | 3 | 23 | | 2019-11-14 22:11:02.429253600 | 2019-11-15 03:24:03.297526800 |
| 5258 | 2019-11-15 00:24:57.483800900 | 10830 | 2019-11-15 00:31:47.959459400 | 11 | 2019-11-15 | 15 | 4 | 0 | | 2019-11-14 22:11:02.429253600 | 2019-11-15 03:24:03.297526800 |
| 5259 | 2019-11-15 00:33:50.642483100 | 10830 | 2019-11-15 00:49:09.138852000 | 11 | 2019-11-15 | 15 | 4 | 0 | 0 days 00:02:02.683023700 | 2019-11-14 22:11:02.429253600 | 2019-11-15 03:24:03.297526800 |
| 5261 | 2019-11-15 01:44:44.293864600 | 10830 | 2019-11-15 01:59:07.159142200 | 11 | 2019-11-15 | 15 | 4 | 1 | 0 days 00:55:35.155012600 | 2019-11-14 22:11:02.429253600 | 2019-11-15 03:24:03.297526800 |
| 5263 | 2019-11-15 02:05:33.623635000 | 10830 | 2019-11-15 02:12:15.581544000 | 11 | 2019-11-15 | 15 | 4 | 2 | 0 days 00:06:26.464492800 | 2019-11-14 22:11:02.429253600 | 2019-11-15 03:24:03.297526800 |
| 5286 | 2019-11-15 17:54:30.140008300 | 10830 | 2019-11-15 18:53:40.433555800 | 11 | 2019-11-15 | 15 | 4 | 18 | 0 days 15:42:14.558464300 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5296 | 2019-11-15 20:57:29.508330200 | 10830 | 2019-11-15 21:02:44.851496700 | 11 | 2019-11-15 | 15 | 4 | 21 | 0 days 02:03:49.074774400 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5297 | 2019-11-15 21:22:24.220020000 | 10830 | 2019-11-15 21:35:19.343132000 | 11 | 2019-11-15 | 15 | 4 | 21 | 0 days 00:19:39.368523300 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5300 | 2019-11-15 21:57:41.880812100 | 10830 | 2019-11-15 22:15:32.520381000 | 11 | 2019-11-15 | 15 | 4 | 22 | 0 days 00:22:22.537680100 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5301 | 2019-11-15 23:00:03.505284200 | 10830 | 2019-11-15 23:15:31.295551700 | 11 | 2019-11-15 | 15 | 4 | 23 | 0 days 00:44:30.984903200 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5302 | 2019-11-15 23:17:00.816945700 | 10830 | 2019-11-15 23:29:21.781482300 | 11 | 2019-11-15 | 15 | 4 | 23 | 0 days 00:01:29.521394000 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| 5303 | 2019-11-15 23:45:16.858411100 | 10830 | 2019-11-16 00:09:29.816557900 | 11 | 2019-11-16 | 16 | 5 | 0 | | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| | 2019-11-16 00:27:19.138782200 | 10830 | 2019-11-16 00:42:06.387264200 | 11 | 2019-11-16 | 16 | 5 | 0 | 0 days 00:17:49.322224300 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| | 2019-11-16 00:57:33.712924800 | 10830 | 2019-11-16 00:58:06.315696400 | 11 | 2019-11-16 | 16 | 5 | 0 | 0 days 00:15:27.325660600 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| | 2019-11-16 01:09:55.180647700 | 10830 | 2019-11-16 01:15:08.916555400 | 11 | 2019-11-16 | 16 | 5 | 1 | 0 days 00:11:48.864951300 | 2019-11-15 16:22:50.725217200 | 2019-11-16 01:58:51.869172700 |
| | 2019-11-16 18:04:30.656633300 | 10830 | 2019-11-16 19:12:32.382217300 | 11 | 2019-11-16 | 16 | 5 | 19 | 0 days 16:49:21.740077900 | 2019-11-16 17:49:44.048717800 | 2019-11-17 01:47:12.073102200 |
+------+-------------------------------+-------+-------------------------------+-------+------------+-----+----------+------+---------------------------+-------------------------------+-------------------------------+
答案 0 :(得分:1)
您可以先在P_Id
列中使用外部联接,然后过滤列之间的匹配-如果可能的话,通过多个匹配获取其中的全部:
df1 = pd.merge(df1, df2, how ='outer', on='P_Id')
df2 = df1[(df1.LogoutTime >= df1.Start) & (df1.LoginTime <= df1.End)]