Getting country name from longitude and latitude

Viewed 502

I would like to extract the country name from the latitude and longitude of the location in my dataframe.

Here is a sample of my data:

{'Country/Region': {0: nan, 1: nan, 2: nan, 3: nan, 4: nan},
         'Lat': {0: '33.93911',
          1: '41.1533',
          2: '28.0339',
          3: '42.5063',
          4: '-11.2027'},
         'Long': {0: '67.709953',
          1: '20.1683',
          2: '1.6596',
          3: '1.5218',
          4: '17.8739'},
         'Province/State': {0: nan, 1: nan, 2: nan, 3: nan, 4: nan}}

Note: this is a pandas dataframe that I only converted to a dictionary for the purpose of asking this question.

This is what I have tried:

df[['Lat','Long']].apply(lambda x,y : geocoder.osm([x,y], method='reverse'),axis=1)

From this I got this error message:

TypeError: ("<lambda>() missing 1 required positional argument: 'y'", 'occurred at index 0')
1 Answers

x in your lambda will be a row from the dataframe, this is what you need to do

df.apply(lambda row : geocoder.osm([row['Lat'], row['Long']], method='reverse'), axis=1)

if your dataset is large consider using pandarallel

Related