我有一个pandas数据框(见下面的输入),其中包含多列,重要的列是start_datetime和end_datetime。我想要应用的算法如下所示。我将每一行与其下一行进行比较。如果end_datetime(当前行)超过了start_datetime(下一行),我想将该行移动到末尾。然后将当前行与之前的第二个下一行进行比较。我希望重复此过程,直到我有一系列时间不重叠的行块。由于将有问题的行移动到末尾,日期时间中将出现一个跳跃,这将作为一个新的不重叠块开始。我有一个带有输入、步骤1、步骤2和输出的示例数据集。
我尝试使用这个for循环来解决问题,但我无法以完成任务的方式进行调整。索引让我感到困惑。也许有一个内置的pandas解决方案可以解决这个问题吗?
输出
我尝试使用这个for循环来解决问题,但我无法以完成任务的方式进行调整。索引让我感到困惑。也许有一个内置的pandas解决方案可以解决这个问题吗?
for i in range(0, df_temp.shape[0] - 1):
if df_temp.iloc[i+1].start_datetime < df_temp.iloc[i].end_datetime:
df_temp = df_temp.reindex([index for index in df_temp.index if index != i + 1] + [i + 1], axis=0)
df_temp.to_csv("output.csv", header=True, index=False)
start_datetime end_datetime
2023-01-01 00:55:13.000 2023-01-01 01:17:56.000
2023-01-01 01:29:57.000 2023-01-01 01:30:24.000
2023-01-01 01:30:55.000 2023-01-01 01:31:33.000
2023-01-01 01:32:05.000 2023-01-01 01:32:44.000
2023-01-01 01:33:59.000 2023-01-01 02:13:24.000
2023-01-01 04:59:59.000 2023-01-01 05:15:48.000
2023-01-01 05:54:04.000 2023-01-01 06:15:53.000
2023-01-01 06:05:56.000 2023-01-01 06:06:02.000 <-- will be shifted to end
2023-01-01 06:06:58.000 2023-01-01 06:42:34.000
2023-01-01 06:08:39.000 2023-01-01 06:31:53.000
2023-01-01 06:16:16.000 2023-01-01 06:39:13.000
2023-01-01 06:30:36.000 2023-01-01 06:38:15.000
2023-01-01 06:32:48.000 2023-01-01 06:49:34.000
2023-01-01 06:35:17.000 2023-01-01 07:02:44.000
2023-01-01 06:59:56.000 2023-01-01 07:47:47.000
2023-01-01 07:17:07.000 2023-01-01 07:20:11.000
2023-01-01 07:22:04.000 2023-01-01 07:57:34.000
2023-01-01 07:24:07.000 2023-01-01 07:54:18.000
2023-01-01 07:36:41.000 2023-01-01 08:07:18.000
2023-01-01 09:24:12.000 2023-01-01 09:46:57.000
2023-01-01 09:26:30.000 2023-01-01 09:58:42.000
2023-01-01 09:31:27.000 2023-01-01 10:00:19.000
步骤1
start_datetime end_datetime
2023-01-01 00:55:13.000 2023-01-01 01:17:56.000
2023-01-01 01:29:57.000 2023-01-01 01:30:24.000
2023-01-01 01:30:55.000 2023-01-01 01:31:33.000
2023-01-01 01:32:05.000 2023-01-01 01:32:44.000
2023-01-01 01:33:59.000 2023-01-01 02:13:24.000
2023-01-01 04:59:59.000 2023-01-01 05:15:48.000
2023-01-01 05:54:04.000 2023-01-01 06:15:53.000
2023-01-01 06:06:58.000 2023-01-01 06:42:34.000 <-- will be shifted to end
2023-01-01 06:08:39.000 2023-01-01 06:31:53.000
2023-01-01 06:16:16.000 2023-01-01 06:39:13.000
2023-01-01 06:30:36.000 2023-01-01 06:38:15.000
2023-01-01 06:32:48.000 2023-01-01 06:49:34.000
2023-01-01 06:35:17.000 2023-01-01 07:02:44.000
2023-01-01 06:59:56.000 2023-01-01 07:47:47.000
2023-01-01 07:17:07.000 2023-01-01 07:20:11.000
2023-01-01 07:22:04.000 2023-01-01 07:57:34.000
2023-01-01 07:24:07.000 2023-01-01 07:54:18.000
2023-01-01 07:36:41.000 2023-01-01 08:07:18.000
2023-01-01 09:24:12.000 2023-01-01 09:46:57.000
2023-01-01 09:26:30.000 2023-01-01 09:58:42.000
2023-01-01 09:31:27.000 2023-01-01 10:00:19.000
2023-01-01 06:05:56.000 2023-01-01 06:06:02.000
第二步
start_datetime end_datetime
2023-01-01 00:55:13.000 2023-01-01 01:17:56.000
2023-01-01 01:29:57.000 2023-01-01 01:30:24.000
2023-01-01 01:30:55.000 2023-01-01 01:31:33.000
2023-01-01 01:32:05.000 2023-01-01 01:32:44.000
2023-01-01 01:33:59.000 2023-01-01 02:13:24.000
2023-01-01 04:59:59.000 2023-01-01 05:15:48.000
2023-01-01 05:54:04.000 2023-01-01 06:15:53.000
2023-01-01 06:08:39.000 2023-01-01 06:31:53.000 <-- will be shifted to end
2023-01-01 06:16:16.000 2023-01-01 06:39:13.000
2023-01-01 06:30:36.000 2023-01-01 06:38:15.000
2023-01-01 06:32:48.000 2023-01-01 06:49:34.000
2023-01-01 06:35:17.000 2023-01-01 07:02:44.000
2023-01-01 06:59:56.000 2023-01-01 07:47:47.000
2023-01-01 07:17:07.000 2023-01-01 07:20:11.000
2023-01-01 07:22:04.000 2023-01-01 07:57:34.000
2023-01-01 07:24:07.000 2023-01-01 07:54:18.000
2023-01-01 07:36:41.000 2023-01-01 08:07:18.000
2023-01-01 09:24:12.000 2023-01-01 09:46:57.000
2023-01-01 09:26:30.000 2023-01-01 09:58:42.000
2023-01-01 09:31:27.000 2023-01-01 10:00:19.000
2023-01-01 06:05:56.000 2023-01-01 06:06:02.000
2023-01-01 06:06:58.000 2023-01-01 06:42:34.000
输出
start_datetime end_datetime
2023-01-01 00:55:13.000 2023-01-01 01:17:56.000 <-- Start of first sequence
2023-01-01 01:29:57.000 2023-01-01 01:30:24.000
2023-01-01 01:30:55.000 2023-01-01 01:31:33.000
2023-01-01 01:32:05.000 2023-01-01 01:32:44.000
2023-01-01 01:33:59.000 2023-01-01 02:13:24.000
2023-01-01 04:59:59.000 2023-01-01 05:15:48.000
2023-01-01 05:54:04.000 2023-01-01 06:15:53.000
2023-01-01 06:16:16.000 2023-01-01 06:39:13.000
2023-01-01 06:59:56.000 2023-01-01 07:47:47.000
2023-01-01 09:24:12.000 2023-01-01 09:46:57.000 <-- End of first sequence without overlap
2023-01-01 06:05:56.000 2023-01-01 06:06:02.000 <-- Start of second sequence
2023-01-01 06:08:39.000 2023-01-01 06:31:53.000
2023-01-01 06:32:48.000 2023-01-01 06:49:34.000
2023-01-01 07:17:07.000 2023-01-01 07:20:11.000
2023-01-01 07:22:04.000 2023-01-01 07:57:34.000
2023-01-01 09:26:30.000 2023-01-01 09:58:42.000 <-- End of second sequence
2023-01-01 06:06:58.000 2023-01-01 06:42:34.000 <-- Start of third sequence
2023-01-01 07:24:07.000 2023-01-01 07:54:18.000
2023-01-01 09:31:27.000 2023-01-01 10:00:19.000 <-- End of third sequence
2023-01-01 06:30:36.000 2023-01-01 06:38:15.000 <-- Start of fourth sequence
2023-01-01 06:35:17.000 2023-01-01 07:02:44.000
2023-01-01 07:36:41.000 2023-01-01 08:07:18.000 <-- End of fourth sequence