使用geopy pandas坐标的新列

时间:2015-07-14 18:27:29

标签: python pandas geopy

我有一个df:

import pandas as pd
import numpy as np
import datetime as DT
import hmac
from geopy.geocoders import Nominatim
from geopy.distance import vincenty

df


     city_name  state_name  county_name
0    WASHINGTON  DC  DIST OF COLUMBIA
1    WASHINGTON  DC  DIST OF COLUMBIA
2    WASHINGTON  DC  DIST OF COLUMBIA
3    WASHINGTON  DC  DIST OF COLUMBIA
4    WASHINGTON  DC  DIST OF COLUMBIA
5    WASHINGTON  DC  DIST OF COLUMBIA
6    WASHINGTON  DC  DIST OF COLUMBIA
7    WASHINGTON  DC  DIST OF COLUMBIA
8    WASHINGTON  DC  DIST OF COLUMBIA
9    WASHINGTON  DC  DIST OF COLUMBIA

我想获取下面数据框中任何一列的纬度和经度坐标。在处理各个位置的文档时,文档(http://geopy.readthedocs.org/en/latest/#data)非常简单。

>>> from geopy.geocoders import Nominatim
>>> geolocator = Nominatim()
>>> location = geolocator.geocode("175 5th Avenue NYC")
>>> print(location.address)
Flatiron Building, 175, 5th Avenue, Flatiron, New York, NYC, New York,     ...
>>> print((location.latitude, location.longitude))
(40.7410861, -73.9896297241625)
>>> print(location.raw)
{'place_id': '9167009604', 'type': 'attraction', ...}

但是我想将函数应用于df中的每一行并创建一个新列。我尝试了以下

df['city_coord'] = geolocator.geocode(lambda row: 'state_name' (row))

但我认为我在代码中遗漏了一些东西,因为我得到以下内容:

    city_name   state_name  county_name coordinates
0    WASHINGTON  DC  DIST OF COLUMBIA    None
1    WASHINGTON  DC  DIST OF COLUMBIA    None
2    WASHINGTON  DC  DIST OF COLUMBIA    None
3    WASHINGTON  DC  DIST OF COLUMBIA    None
4    WASHINGTON  DC  DIST OF COLUMBIA    None
5    WASHINGTON  DC  DIST OF COLUMBIA    None
6    WASHINGTON  DC  DIST OF COLUMBIA    None
7    WASHINGTON  DC  DIST OF COLUMBIA    None
8    WASHINGTON  DC  DIST OF COLUMBIA    None
9    WASHINGTON  DC  DIST OF COLUMBIA    None

我希望这样的东西可以使用Lambda函数:

     city_name  state_name  county_name  city_coord
0    WASHINGTON  DC  DIST OF COLUMBIA    38.8949549, -77.0366456 
1    WASHINGTON  DC  DIST OF COLUMBIA    38.8949549, -77.0366456 
2    WASHINGTON  DC  DIST OF COLUMBIA    38.8949549, -77.0366456 
3    WASHINGTON  DC  DIST OF COLUMBIA    38.8949549, -77.0366456 
4    WASHINGTON  DC  DIST OF COLUMBIA    38.8949549, -77.0366456 
5    WASHINGTON  DC  DIST OF COLUMBIA    38.8949549, -77.0366456 
6    WASHINGTON  DC  DIST OF COLUMBIA    38.8949549, -77.0366456 
7    WASHINGTON  DC  DIST OF COLUMBIA    38.8949549, -77.0366456 
8    WASHINGTON  DC  DIST OF COLUMBIA    38.8949549, -77.0366456 
9    WASHINGTON  DC  DIST OF COLUMBIA    38.8949549, -77.0366456
10   GLYNCO      GA  GLYNN               31.2224512, -81.5101023

我感谢任何帮助。在我得到坐标后,我想要映射它们。任何推荐的映射坐标资源也非常受欢迎。感谢

2 个答案:

答案 0 :(得分:12)

你可以调用apply并在每一行上传递你想要执行的功能,如下所示:

In [9]:

geolocator = Nominatim()
df['city_coord'] = df['state_name'].apply(geolocator.geocode)
df
Out[9]:
    city_name state_name       county_name  \
0  WASHINGTON         DC  DIST OF COLUMBIA   
1  WASHINGTON         DC  DIST OF COLUMBIA   

                                          city_coord  
0  (District of Columbia, United States of Americ...  
1  (District of Columbia, United States of Americ...  

然后,您可以访问纬度和经度属性:

In [16]:

df['city_coord'] = df['city_coord'].apply(lambda x: (x.latitude, x.longitude))
df
Out[16]:
    city_name state_name       county_name                       city_coord
0  WASHINGTON         DC  DIST OF COLUMBIA  (38.8937154, -76.9877934586326)
1  WASHINGTON         DC  DIST OF COLUMBIA  (38.8937154, -76.9877934586326)

或者通过拨打apply两次在一个班轮中进行:

In [17]:
df['city_coord'] = df['state_name'].apply(geolocator.geocode).apply(lambda x: (x.latitude, x.longitude))
df

Out[17]:
    city_name state_name       county_name                       city_coord
0  WASHINGTON         DC  DIST OF COLUMBIA  (38.8937154, -76.9877934586326)
1  WASHINGTON         DC  DIST OF COLUMBIA  (38.8937154, -76.9877934586326)

此外,您的尝试geolocator.geocode(lambda row: 'state_name' (row))没有做任何事情,因此为什么您的列中包含None个值

修改

@leb在这里提出了一个有趣的观点,如果您有许多重复值,那么对每个唯一值进行地理编码会更加高效,然后添加:

In [38]:
states = df['state_name'].unique()
d = dict(zip(states, pd.Series(states).apply(geolocator.geocode).apply(lambda x: (x.latitude, x.longitude))))
d

Out[38]:
{'DC': (38.8937154, -76.9877934586326)}

In [40]:    
df['city_coord'] = df['state_name'].map(d)
df

Out[40]:
    city_name state_name       county_name                       city_coord
0  WASHINGTON         DC  DIST OF COLUMBIA  (38.8937154, -76.9877934586326)
1  WASHINGTON         DC  DIST OF COLUMBIA  (38.8937154, -76.9877934586326)

所以上面使用unique获取所有唯一值,从它们构造一个dict,然后调用map来执行查找并添加coords,这比尝试地理编码行更有效-wise

答案 1 :(得分:4)

Upvote并接受@ EdChum的回答,我只想补充一点。他的方法很完美,但从个人经验来看,我想分享一些事情:

在处理地理编码时,如果您有多个正在重复的城市/州组合,那么更快只发送1来进行地理编码,然后将其余部分复制到下面的其他行:

这对非常有帮助,可以通过两种方式完成大数据:

  1. 仅基于您的数据,因为行似乎完全重复,并且只有在您需要时,删除额外的行并对其中一行执行地理编码。这可以使用drop_duplicate
  2. 完成
  3. 如果您希望保留所有行,group_by城市/州组合,请通过调用head(1)对第一个行应用地理编码,然后复制到其余行。
  4. 原因是每次你打电话给Nominatim时,即使你连续排队同一个城市/州,也存在一个小的延迟问题。当您的数据变大导致响应的大量延迟和可能的超时时,此延迟会变得更糟。

    同样,这一切都来自于个人处理它。如果它现在对您没有好处,请记住以备将来使用。