如何收集位置数据框架的经纬度数据列表?



我最近开始用Python编程。我正在导入一个csv文件,其中包含印度各地的848个独特的位置,并希望使用geopy模块为每个位置添加纬度和经度。导入数据后,我使用以下代码:

from geopy.geocoders import Nominatim
import pandas as pd
df = pd.read_csv("../project/LocationIndia.csv")
lat = []
long = []
for location in df["Location"]:
# Initialize Nominatim API
address = location + ', India'
geolocator = Nominatim(user_agent="MyApp")
location = geolocator.geocode(address)
lat.append(location.latitude)
long.append(location.longitude)
df['latitude']=latitude
df['longitude']=longitude
df

输出:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [14], in <cell line: 4>()
7     geolocator = Nominatim(user_agent="MyApp")
8     location = geolocator.geocode(address)
----> 9     lat.append(location.latitude)
10     long.append(location.longitude)
11 df['latitude']=latitude
AttributeError: 'NoneType' object has no attribute 'latitude'

不知道为什么会这样。如果有人能帮助我,将是伟大的,谢谢!

这是表格:

Location
0   Kharghar
1   Sector-13 Kharghar
2   Sector 18 Kharghar
3   Sector 20 Kharghar
4   Sector 15 Kharghar
... ...
843 BTM Layout
844 Kuvempu Layout on Hennur Main Road
845 Marathahalli
846 Rajajinagar
847 RMV

我期望在数据框中以列的形式输入值。

geolocator.geocode()如果没有找到结果,则返回None。尝试如下错误处理:

if location is not None:
# your code here

最新更新