I have a data frame of latitude and longitude coordinates on this CSV file:Longlat.
I used this code to try to get the zipcodes:
import copy
def get_zipcode(df, geolocator, lat_field, lon_field):
location = geolocator.reverse((df[lat_field], df[lon_field]))
return location.raw['address']['postcode']
geolocator = geopy.Nominatim(user_agent='myusername') #My OpenMap username
zipcodes = longlat.apply(get_zipcode, axis=1, geolocator=geolocator, lat_field=longlat['LATITUDE_X'], lon_field=longlat['LONGITUDE_X'])
I got the error:
KeyError
"None of [Float64Index([39.0962320000896, 39.1462010000896, 39.1347670000896,\n 39.1076250000897, 39.0928490000897, 39.1648900000896,\n 39.1846440000895, 39.0970790000897, 39.1491220000896,\n 39.1145560000896,\n ...\n 39.1039560000896,
How do I fix it?
import geopy
import pandas as pd
longlat = pd.read_csv("longlat.csv",encoding = 'utf-8', sep = '\t')
geolocator = geopy.Nominatim(user_agent="check_1")
def get_zip_code(x):
location = geolocator.reverse("{}, {}".format(x['LATITUDE_X'],x['LONGITUDE_X']))
return location.raw['address']['postcode']
longlat['zipcode'] = longlat.head().apply(lambda x: get_zip_code(x), axis = 1)
print(longlat.head())
Unnamed: 0 LATITUDE_X LONGITUDE_X zipcode
0 124148 39.096232 -84.653337 45233-4555
1 124209 39.146201 -84.468843 45207
2 125298 39.134767 -84.499079 45267
3 125299 39.107625 -84.496675 45202
4 125390 39.092849 -84.388332 45230