如何从Excel Doc(Python)的地址阅读中找到经度和纬度?
我正在尝试阅读包含地址的Excel Doc,然后找到相应的经度和纬度。我遇到了一个错误,不确定如何继续。获取错误:“'nontype'对象没有属性'latitude'”。任何帮助将不胜感激! 是代码:
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)
[这是我正在阅读的excel表格]
这 我通过输入手动地址运行它,在此处看到:
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)
输出:
位置location_lat location_long
0 188 glezen lane lane Wayland ma 01778 42.386672 -71.345426
I am trying to read an excel doc that contains addresses and then find the corresponding longitude and latitudes. I am running into an error and not sure how to continue. Getting error: "'NoneType' object has no attribute '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]
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
0 188 Glezen Lane Wayland MA 01778 42.386672 -71.345426
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您过于复杂的事情……这是一项足够简单的任务 - 列出地址列表并通过函数运行。这样的事情应该做到这一点:
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: