[英]How to find Longitude and Latitude from an Address Reading from an Excel doc (Python)?
I am trying to read an excel doc that contains addresses and then find the corresponding longitude and latitudes.我正在尝试阅读包含地址的 excel 文档,然后找到相应的经度和纬度。 I am running into an error and not sure how to continue.
我遇到了一个错误,不知道如何继续。 Getting error: "'NoneType' object has no attribute 'latitude'".
出现错误:“'NoneType' 对象没有属性 'latitude'”。 Any help would be appreciated!
任何帮助,将不胜感激! Here is the code:
这是代码:
import pandas as pd
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="myApp")
data = pd.read_excel (r'C:\Users\scott.correia\Desktop\SoS_Test.xlsx')
df2 = pd.DataFrame(data, columns = ['Location'])
df2[['location_lat', 'location_long']] = df2['Location'].apply(
geolocator.geocode).apply (lambda x: pd.Series(
[x.latitude, x.longitude], index = ['location_lat', 'location_long']))
print(df2)
[HERE IS WHAT EXCEL SHEET LOOKS LIKE I AM READING] [这是我正在阅读的 EXCEL 表格]
When I run it with inputting a manual address it works seen here:当我输入手动地址运行它时,它可以在这里看到:
import pandas as pd
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="myApp")
df2 = pd.DataFrame({'Location':
['188 Glezen Lane Wayland MA 01778']})
df2[['location_lat', 'location_long']] = df2['Location'].apply(
geolocator.geocode).apply (lambda x: pd.Series(
[x.latitude, x.longitude], index = ['location_lat', 'location_long']))
print(df2)
OUTPUT:输出:
Location location_lat location_long位置 location_lat location_long
0 188 Glezen Lane Wayland MA 01778 42.386672 -71.345426 0 188 Glezen Lane Wayland MA 01778 42.386672 -71.345426
It seems you are over-complicating things a little... This is a simple enough task - take a list of addresses and run it through a function.看起来你有点过于复杂了......这是一个足够简单的任务 - 获取地址列表并通过函数运行它。 Something like this should do it:
这样的事情应该这样做:
addresses = pd.read_excel (data_path)["Location"]
coords = zip(*map(lambda addr: (addr.latitude, addr.longitude),
addresses))
data = pd.DataFrame({"Location" : addresses,
"Latitude" : coords[0],
"Longitude" : coords[1]})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.