将反向地理编码功能应用于所有列的Python熊猫过长?

时间:2019-03-06 12:39:29

标签: python pandas reverse-geocoding

我正在尝试使用此库将4列反向地理编码为位置名称。 https://github.com/thampiman/reverse-geocoder 代码有效,但是即使20行也要花费30秒左右,我有100.000行以上,所以要花很多时间。我想知道为什么会这样吗?

示例数据

pickup_longitude pickup_latitude dropoff_longitude dropoff_latitude
-73.982155 40.767937 -73.964630 40.765602
-73.981049 40.744339 -73.973000 40.789989

结果:

 pickup_longitude pickup_latitude dropoff_longitude dropoff_latitude pickup_district dropoff_district
    -73.982155 40.767937 -73.964630 40.765602 Manhattan Manhattan
    -73.981049 40.744339 -73.973000 40.789989 Long Island City Manhattan

代码:

ds['pickup_district'] = ds.apply(lambda row: rg.search((row['pickup_latitude'],row['pickup_longitude']))[0]['name'],axis=1)
ds['dropoff_district'] = ds.apply(lambda row: rg.search((row['dropoff_latitude'],row['dropoff_longitude']))[0]['name'],axis=1)

再加上巴斯马丹geçmeyinsincaplar;)

2 个答案:

答案 0 :(得分:1)

您当前的结构正在为rg.search中的每一行调用一次DataFrame方法。

先创建一个元组列表,然后一次调用rg.search进行下放,再调用一次进行提取,效率会更高。例如:

pickup_coords = ds[['pickup_latitude', 'pickup_longitude']].apply(tuple, axis=1).tolist()
dropoff_coords = ds[['dropoff_latitude', 'dropoff_longitude']].apply(tuple, axis=1).tolist()

pickup_results = rg.search(pickup_coords, mode=2)
ds['pickup_district'] = [x['name'] for x in pickup_results]

dropoff_results = rg.search(dropoff_coords, mode=2)
ds['dropoff_district'] = [x['name'] for x in dropoff_results]

答案 1 :(得分:1)

您可以一次调用所有位置的图书馆。例如:

pickups = list(zip(ds.pickup_latitude, ds.pickup_longitude))
dropoffs = list(zip(ds.dropoff_latitude, ds.dropoff_longitude))

pickup_locations = rg.search(pickups)
dropoff_locations = rg.search(dropoffs)

ds['pickup_district'] = [p["name"] for p in pickup_locations]
ds['dropoff_district'] = [d["name"] for d in dropoff_locations]

这比调用每一行要快得多(就像套用一样)。