python - 限制python中2个持续时间之间的时间戳列?
问题描述
我有以下数据框。
df = {'location_id': [1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5],
'timestamp':['2020-05-26 06:00:00','2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00',
'2020-05-26 10:00:00','2020-05-26 11:00:00','2020-05-26 12:00:00', '2020-05-26 13:00:00',
'2020-05-26 06:00:00','2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00',
'2020-05-26 10:00:00','2020-05-26 11:00:00','2020-05-26 12:00:00', '2020-05-26 13:00:00',
'2020-05-26 06:00:00','2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00',
'2020-05-26 10:00:00','2020-05-26 11:00:00','2020-05-26 12:00:00', '2020-05-26 13:00:00',
'2020-05-26 06:00:00','2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00',
'2020-05-26 10:00:00','2020-05-26 11:00:00','2020-05-26 12:00:00', '2020-05-26 13:00:00',
'2020-05-26 06:00:00','2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00',
'2020-05-26 10:00:00','2020-05-26 11:00:00','2020-05-26 12:00:00', '2020-05-26 13:00:00']
}
dataframe = pd.DataFrame(df)
每个 location_id 都有一个从 2020-05-06 06:00:00 到 2020-05-26 13:00:00 的时间戳。我想要实现的是限制每个 location_id 的时间戳,以便所有 id 的时间戳仅在 07:00:00 和 10:00:00 之间。
最终数据框应如下所示:
df = {'location_id': [1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5],
'timestamp':['2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', '2020-05-26 10:00:00',
'2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', '2020-05-26 10:00:00',
'2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', '2020-05-26 10:00:00',
'2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', '2020-05-26 10:00:00',
'2020-05-26 07:00:00','2020-05-26 08:00:00', '2020-05-26 09:00:00', '2020-05-26 10:00:00']
}
dataframe = pd.DataFrame(df)
到目前为止,我尝试的是以下内容:
dataframe[(dataframe['timestamp'] >= '2020-05-26 07:00:00') & (dataframe['timestamp'] <= '2020-05-26 10:00:00')]
在这个例子中,它可以工作,但是在大型数据集上应用它时,它并没有给我正确的答案。我尝试根据 location_id 和时间戳对值进行排序,然后应用相同的条件,但仍然无法正常工作。它总是占用整个数据帧。如何实现?请注意,这是取自一个非常大的数据集的示例。(13,000,000 行和 2 列,每个 location_id 的实际时间戳在 2020-05-26 00:00:00 到 2020-07-05 23:00:00 之间) . 如果有人能给我一个解决方案,我将不胜感激:)!
解决方案
set_index
在您的时间列上并使用between_time
:
df = pd.DataFrame(df)
df["timestamp"] = pd.to_datetime(df["timestamp"])
print (df.set_index("timestamp").between_time("07:00","11:00"))
location_id
timestamp
2020-05-26 07:00:00 1
2020-05-26 08:00:00 1
2020-05-26 09:00:00 1
2020-05-26 10:00:00 1
2020-05-26 11:00:00 1
2020-05-26 07:00:00 2
2020-05-26 08:00:00 2
2020-05-26 09:00:00 2
2020-05-26 10:00:00 2
2020-05-26 11:00:00 2
2020-05-26 07:00:00 3
2020-05-26 08:00:00 3
2020-05-26 09:00:00 3
2020-05-26 10:00:00 3
2020-05-26 11:00:00 3
2020-05-26 07:00:00 4
2020-05-26 08:00:00 4
2020-05-26 09:00:00 4
2020-05-26 10:00:00 4
2020-05-26 11:00:00 4
2020-05-26 07:00:00 5
2020-05-26 08:00:00 5
2020-05-26 09:00:00 5
2020-05-26 10:00:00 5
2020-05-26 11:00:00 5
推荐阅读
- database - 我收到“预期 1 个位置参数,但找到 0 个”。使用 Dart 和 Cloud Firestore 时 Flutter 出错?
- python - python - 如何使用join方法和sort方法
- performance - Data.Map 是二叉搜索树的最佳数据类型吗?
- python - 如何在二进制图像中找到特定点?
- javascript - 猫鼬`findById`不返回预期的文件
- c++ - C ++多索引列csv加载
- wordpress - 每月在 woocommerce 优惠券中重置每位用户的使用限制
- android - firestore getDocuments().size() != getDocumentChanges().size()
- kdb - 将时间戳四舍五入到最接近的 30 秒
- c++ - 消失的 SDL 矩形。如何使用新的附加形状更新窗口