gpt4 book ai didi

python - 如何将公交车进/出站记录汇总到行程中,赋予唯一ID,并剔除时间差异小的重复记录?

转载 作者:行者123 更新时间:2023-12-04 17:21:22 25 4
gpt4 key购买 nike

我每天有一条公交线路的数据集,有 32 辆公交车和两辆 route_direction(0,1),在第一个方向上有 18 个车站,每个车站都有一个从 1 到 18 的序列,而其他方向有15个站,seq(1-15),记录进/出每个站的时间。每条记录包含bus_id、route_direction、station_seq、in_time、out_time、station_id。 enter image description here

route_id    route_direction bus_id  station_seq schdeule_date   in_time out_time

0 59 1 1349508393 2 2021-01-01 05:04:31 05:04:58

1 59 1 1349508393 2 2021-01-01 05:04:27 05:04:58

2 59 1 1349508393 2 2021-01-01 05:04:31 05:06:31

3 59 1 1349508393 2 2021-01-01 05:04:27 05:06:31

4 59 1 1349508393 1 2021-01-01 05:00:35 05:00:56

首先,我尝试对某些列进行分组,以便为​​每次旅行提供索引:

grouped = df.groupby(['bus_id', 'route_direction'])

我在这张图片中得到了类似的东西 enter image description here :

index   route_id    route_direction bus_id  station_seq schdeule_date   in_time out_time

654 59 0 1349508329 1 2021-01-01 NaN 06:34:10

663 59 0 1349508329 2 2021-01-01 06:33:34 06:34:04

664 59 0 1349508329 2 2021-01-01 06:33:33 06:34:04

677 59 0 1349508329 2 2021-01-01 06:33:34 06:35:34

678 59 0 1349508329 2 2021-01-01 06:33:33 06:35:34

... ... ... ... ... ... ... ...

12133 59 0 1349508329 12 2021-01-01 NaN NaN

如您所见,在几乎相同的日期和时间,相同的 bus_id 在同一车站的 enter exit 也有重复:我试过删除重复项但运气不好:

df = df.drop_duplicates(subset=['bus_id', 'route_direction', 'station_seq', 'station_id', 'in_time'], keep='first').reset_index(drop=True)

在 in_time 或 out_time 中也有一些 NaN 值,所以如果我 dropna 那么我可能会错过公交线路沿线站点之一的记录。

对一次旅行中的每条公共(public)汽车记录进行分组以赋予其 ID 有何帮助?在这种情况下如何删除重复的记录(输入时间略有不同)?任何帮助将不胜感激。

最佳答案

  1. 使用“bus_id”和“in_time”对值进行排序
  2. groupby 'bus_id',对于每个 bus_id,计算每条记录与其之前记录的时间差
  3. 如果时间差小于60s,则标记为0,否则标记为1,以设置某些组忽略时间差<60s
  4. 在标签上使用cumsum,创建组标签
  5. groupby grouptag,对于每个grouptag保持最小(in_time)和最大(out_time)
# convert the in_time to dateTime first, then sorted the values
df['in_time_t'] = pd.to_datetime(df['schdeule_date'] + ' ' + df['in_time'])
df.sort_values(['bus_id', 'in_time_t'], inplace=True)

# calculate the time difference for every bus_id
df['t_diff'] = df.groupby('bus_id')['in_time_t'].diff()

# set group_tag
cond = df['t_diff'].dt.seconds < 60
df['tag'] = np.where(cond, 0, 1).cumsum()

# for every grouptag keep min(in_time) and max(out_time)
df_result = df.groupby(['route_id', 'route_direction', 'bus_id', 'station_seq', 'schdeule_date',
'tag']).agg({'in_time':'min', 'out_time':'max'}).reset_index()
df
route_id route_direction bus_id station_seq schdeule_date in_time out_time
0 59 1 1349508393 2 2021-01-01 05:04:31 05:04:58
1 59 1 1349508393 2 2021-01-01 05:04:27 05:04:58
2 59 1 1349508393 2 2021-01-01 05:04:31 05:06:31
3 59 1 1349508393 2 2021-01-01 05:04:27 05:06:31
4 59 1 1349508393 1 2021-01-01 05:00:35 05:00:56
654 59 0 1349508329 1 2021-01-01 NaN 06:34:10
663 59 0 1349508329 2 2021-01-01 06:33:34 06:34:04
664 59 0 1349508329 2 2021-01-01 06:33:33 06:34:04
677 59 0 1349508329 2 2021-01-01 06:33:34 06:35:34
678 59 0 1349508329 2 2021-01-01 06:33:33 06:35:34
12133 59 0 1349508329 12 2021-01-01 NaN NaN

df_result
route_id route_direction bus_id station_seq schdeule_date tag in_time out_time
0 59 0 1349508329 1 2021-01-01 2 NaN 06:34:10
1 59 0 1349508329 2 2021-01-01 1 06:33:33 06:35:34
2 59 0 1349508329 12 2021-01-01 3 NaN NaN
3 59 1 1349508393 1 2021-01-01 4 05:00:35 05:00:56
4 59 1 1349508393 2 2021-01-01 5 05:04:27 05:06:31

df with tag
| | route_id | route_direction | bus_id | station_seq | schdeule_date | in_time | out_time | in_time_t | t_diff | tag |
|------:|-----------:|------------------:|-----------:|--------------:|:----------------|:----------|:-----------|:--------------------|:----------------|------:|
| 664 | 59 | 0 | 1349508329 | 2 | 2021-01-01 | 06:33:33 | 06:34:04 | 2021-01-01 06:33:33 | NaT | 1 |
| 678 | 59 | 0 | 1349508329 | 2 | 2021-01-01 | 06:33:33 | 06:35:34 | 2021-01-01 06:33:33 | 0 days 00:00:00 | 1 |
| 663 | 59 | 0 | 1349508329 | 2 | 2021-01-01 | 06:33:34 | 06:34:04 | 2021-01-01 06:33:34 | 0 days 00:00:01 | 1 |
| 677 | 59 | 0 | 1349508329 | 2 | 2021-01-01 | 06:33:34 | 06:35:34 | 2021-01-01 06:33:34 | 0 days 00:00:00 | 1 |
| 654 | 59 | 0 | 1349508329 | 1 | 2021-01-01 | nan | 06:34:10 | NaT | NaT | 2 |
| 12133 | 59 | 0 | 1349508329 | 12 | 2021-01-01 | nan | nan | NaT | NaT | 3 |
| 4 | 59 | 1 | 1349508393 | 1 | 2021-01-01 | 05:00:35 | 05:00:56 | 2021-01-01 05:00:35 | NaT | 4 |
| 1 | 59 | 1 | 1349508393 | 2 | 2021-01-01 | 05:04:27 | 05:04:58 | 2021-01-01 05:04:27 | 0 days 00:03:52 | 5 |
| 3 | 59 | 1 | 1349508393 | 2 | 2021-01-01 | 05:04:27 | 05:06:31 | 2021-01-01 05:04:27 | 0 days 00:00:00 | 5 |
| 0 | 59 | 1 | 1349508393 | 2 | 2021-01-01 | 05:04:31 | 05:04:58 | 2021-01-01 05:04:31 | 0 days 00:00:04 | 5 |
| 2 | 59 | 1 | 1349508393 | 2 | 2021-01-01 | 05:04:31 | 05:06:31 | 2021-01-01 05:04:31 | 0 days 00:00:00 | 5 |

关于python - 如何将公交车进/出站记录汇总到行程中,赋予唯一ID,并剔除时间差异小的重复记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66002220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com