返回在数据帧中每个位置的 100 米边界内停放滑板车的用户的每日报告



我正在使用 2 个数据帧。

rent_aug_df看起来像:

end_time                lng        lat
0   2019-08-01 05:16:12  127.048667  37.504856
1   2019-08-01 05:39:18  126.934945  37.558505
2   2019-08-01 05:39:47  126.889154  37.581799
3   2019-08-01 05:44:03  0.000000    0.000000
4   2019-08-01 06:00:13  127.067109  37.543945

cu_nodes_df看起来像:

id  title            created_at              lng      lat
0   location_1  2019-01-16 21:21:11     127.03338   37.486277
1   location_2  2019-02-15 20:54:59     127.11021   37.402250
2   location_3  2019-02-22 17:57:02     126.93289   37.519600
3   location_4  2019-02-26 21:58:27     127.04459   37.524680
4   location_5  2019-02-26 21:58:55      127.02592  37.518500

目标: 循环遍历 cu_nodes_df["title"] 中的每个location_i,并将其地理编码与rent_aug_df行中的每个地理编码进行比较。

这是我的代码:

from haversine import haversine, Unit
data = {}
for node in cu_nodes_df["title"]:
# (lat, lng) for current cu_node, getting row where title match.
geo_df = cu_nodes_df.loc[cu_nodes_df["title"] == node][["lng", "lat"]]
cu_geocode = (geo_df.T.values[0][0], geo_df.T.values[1][0])
# x = each row of (lat,lng) in rent_aug_df 
df = pd.DataFrame(rent_aug_df[(rent_aug_df[['lat','lng']].apply(lambda x: haversine(x, cu_geocode), axis=1)) <= 0.1])
print(df.head())
print(type(cu_geocode[0]))
print(cu_geocode)
print(df.dtypes)
df.set_index('end_time', inplace = True)
data[node] = list(df.groupby(df.index.date)["end_loc"].count())
final = pd.DataFrame(data=data)

打印输出:

Empty DataFrame
Columns: [end_loc, end_time, lng, lat]
Index: []
<class 'numpy.float64'>
(127.03338, 37.486277)
end_loc             object
end_time    datetime64[ns]
lng                float64
lat                float64
dtype: object

我无法找出代码的问题。我已经尝试了我能想到的大多数替代方案。我尝试过单cu_geocode:

df = pd.DataFrame(rent_aug_df[(rent_aug_df[['lat','lng']].apply(lambda x: haversine(x, (37.504855525623, 127.04866656867)), axis=1)) <= 0.1])

这工作正常,创建由租用的踏板车组成的df,这些踏板车在0.1公里边界内返回

为什么它在 forloop 中不起作用? 提前致谢

编辑:我有(lng,lat(而不是(lat,lng(,当我改变它时,它可以工作。

只是代码可读性的提示。

# Augment a new column to have geocode tuple
df['geo_code'] = df.apply(lambda x: (x['lng'], x['lat']))
# Convert to a dict
new_df = df.filter(items['title', 'geo_code'])
data = new_df.to_dict()

最新更新