I have a code with pandas and geopy that obtains a dataframe from excel and obtains locations from coordinates (its slow).
import pandas as pd
import geopy as gp
geolocator = gp.geocoders.Nominatim(user_agent="Fortune")
BC = pd.read_excel("MEXSCTv2.xlsx",sheet_name='BC')
BC['coord']=BC['LATITUD'].astype("string")+","+BC['LONGITUD'].astype("string")
BC['location'] = BC['coord'].apply(geolocator.reverse)
BC[['coord','location']]
gives:
coord location
0 31.874957,-116.56535 (Carretera Ensenada-San Felipe, Ensenada, Muni...
1 31.887848,-116.504794 (Carretera Ensenada-San Felipe, Ensenada, Muni...
2 31.887947,-116.492945 (Carretera Ensenada-San Felipe, Ensenada, Muni...
3 31.888869,-116.280557 (Carretera Ensenada-San Felipe, Rancho Agua Vi...
4 31.848649,-116.075738 (Carretera Ensenada-San Felipe, Rancho los Man...
... ... ...
242 32.406411,-115.194782 (Carretera Mexicali-Estación Coahuila, Ejido N...
243 32.31572,-115.189541 (Carretera Mexicali-Estación Coahuila, Municip...
244 32.243859,-115.167499 (Carretera Mexicali-Estación Coahuila, Las Pal...
245 32.215375,-115.167491 (Carretera Mexicali-Estación Coahuila, Colonia...
246 32.184844,-115.013685 (Carretera Mexicali-Estación Coahuila, Colonia...
i want this code to get only the state (all the locations I will get in my codes are in Mexico)
i found a way to obtain the state from individual locations but not for dataframes.
geolocator.reverse(BC['coord'][0]).raw['address']['state']
gives:
'Baja California'
pls help, im not really familiar with apply
Solved!
though there are some coordinates that dont have a state, but i hope it will be usefull for someone