Hi!
I am using geopy Nominatim in python to retrieve city names, province and regions of some postcodes.
I have been able to get accurate city names using coutry_codes, but now I am having problems with provinces.
The problem is that I don't have the province in my raw['address'] field. I learned that I should use admin_level = 6 to get the province in my output but I don't know how to use it.
This is the code:
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter
geolocator = Nominatim()
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
Locations = pd.read_csv("geoloc1.csv", delimiter = ';')
for index, row in Locations.iterrows():
time.sleep(1)
country = np.where(countries == row['NAZIONE'])[0][0] # Finding the index of the corresponding country
country = switch_country(country) # Finding the correct code for the country
#print(nazione)
if country == 'it':
loca = geocode(query = row['CAP'], country_codes = country, addressdetails = True, language = 'it,en')
try:
row['PROVINCIA'] = loca.raw['address']['county']
except:
row['PROVINCIA'] = '_'
try:
row['REGIONE'] = loca.raw['address']['state']
except:
row['REGIONE'] = '_'
loca = geocode(query = row['CAP'], country_codes = country, addressdetails = True, language = 'it,en')
try:
row['LOCATION'] = loca.raw['address']['city']
Cities.append(loca.raw['address'])
except:
try:
row['LOCATION'] = loca.raw['address']['hamlet']
Cities.append(loca.raw['address'])
except:
try:
row['LOCATION'] = loca.raw['address']['village']
Cities.append(loca.raw['address'])
except:
row['LOCATION'] = '_'
Cities.append(loca)
Output = Output.append(row)
How can I use admin_level = 6 to get the province name in my raw['address']?
Thanks,
Carlotta.
asked
06 Apr '20, 08:03
Carlotta
21●4●4●9
accept rate:
0%