如何从Excel Doc(Python)的地址阅读中找到经度和纬度?

发布于 2025-02-13 17:59:24 字数 1193 浏览 2 评论 0原文

我正在尝试阅读包含地址的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] 1

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仲春光 2025-02-20 17:59:25

看来您过于复杂的事情……这是一项足够简单的任务 - 列出地址列表并通过函数运行。这样的事情应该做到这一点:

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]})

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]})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文