【发布时间】:2022-01-21 15:22:18
【问题描述】:
我有公交车站到达预测的数据框:
path_id | forecast | forecast_made_at | bus_id
int | datetime | datetime | int
我们每 5 分钟进行一次预测,因此可以复制数据库条目。例如
In 11:50 we predict bus #11544 will arrive at 11:59
In 11:50 we predict bus #95447 will arrive at 11:55
--......--
In 11:55 we predict bus #11544 will arrive at 12:02
我想获得具有最大 forecast_made_at 参数的最新预测:
res = pd.DataFrame()
for k, row in t_data.iterrows():
prediction = dict(**row)
forecasts = t_data[t_data["bus_id"] == prediction["bus_id"]] # Forecasts with the same bus_id
prediction["best"] = (prediction["forecast_made_at"] == max(forecasts["forecast_made_at"]))
res = res.append(prediction, ignore_index=True)
res = res[res["best"] == True]
在这段代码中,我们使用的是字典而不是 pandas 对象,所以这个非常慢。如何使用熊猫工具做到这一点
【问题讨论】:
-
你能提供一些你的数据框行吗?
-
(55, 12:07, 12:00, 1231), (55, 12:11, 12:00, 1789), (55, 12:08, 12:05:, 1231)
标签: python pandas dataframe data-science