简体   繁体   English

如何从 Excel 文档(Python)读取的地址中查找经度和纬度?

[英]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 表格]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位置 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.

相关问题 如何快速从python中的地址获取经纬度? - How to get the latitude and longitude from the address in python quickly? 如何在python中从经纬度找到人口普查区 - How to find census tract from latitude and longitude in python 将地址到经纬度的地理编码结果保存到Python中的原始dataframe - Save geocoding results from address to longitude and latitude to original dataframe in Python 如何从Django GeoIP中的经度和纬度获取地址? - How to get Address from Latitude & Longitude in Django GeoIP? 从地址地理位置获取纬度和经度 - Get latitude & longitude from address geopandas 从Mapbox刮取地址的经纬度 - Scrape latitude and longitude of address obtained from Mapbox 从 python/pandas 中的经纬度数据中查找最近的医院 - Find nearest hospital from latitude and longitude data in python/pandas 如何在距海岸线固定距离处找到点(纬度,经度)? - How to find points(latitude, longitude) at a fixed distance from the coast line? 如何在谷歌中将经纬度数据框放置为python的api - How to make a dataframe from latitude and longitude in google places api for python 如何从 python 的经度和纬度获取邮政编码? - How do I get zipcodes from longitude and latitude on python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM