【发布时间】:2021-01-13 17:14:17
【问题描述】:
自从我上一篇文章确实缺乏信息:
我的 df 示例(重要的 col): deviceID:车辆的唯一 ID。车辆在 X 分钟内发送数据。 里程:自上一条消息以来移动的距离(以公里为单位) positon_timestamp_measure:数据集创建时间的unixTimestamp。
deviceID mileage positon_timestamp_measure
54672 10 1600696079
43423 20 1600696079
42342 3 1600701501
54672 3 1600702102
43423 2 1600702701
我的目标是通过使用时间戳和里程计算车辆的速度,将里程与车辆的最大速度(即 80 公里/小时)进行比较来验证里程。然后将结果写入原始数据集中。
到目前为止,我所做的如下:
df_ori['dataIndex'] = df_ori.index
df = df_ori.groupby('device_id')
#create new col and set all values to false
df_ori['valid'] = 0
for group_name, group in df:
#sort group by time
group = group.sort_values(by='position_timestamp_measure')
group = group.reset_index()
#since I can't validate the first point in the group, I set it to valid
df_ori.loc[df_ori.index == group.dataIndex.values[0], 'validPosition'] = 1
#iterate through each data in the group
for i in range(1, len(group)):
timeGoneSec = abs(group.position_timestamp_measure.values[i]-group.position_timestamp_measure.values[i-1])
timeHours = (timeGoneSec/60)/60
#calculate speed
if((group.mileage.values[i]/timeHours)<maxSpeedKMH):
df_ori.loc[dataset.index == group.dataIndex.values[i], 'validPosition'] = 1
dataset.validPosition.value_counts()
它确实按我想要的方式工作,但是它在性能方面缺乏很多。 df 包含近 700k 的数据(已清理)。我仍然是初学者,无法找到更好的解决方案。非常感谢您的帮助。
【问题讨论】:
标签: python pandas dataframe loops pandas-groupby