0

I'm new to Python and using Pandas dataframes.

I'm trying to create a new column in a dataframe of country names composed from latitude and longitude records via reverse geocoding, but I'm having some troubles.

This works perfectly, and returns name of country for one row:

res = Geocoder.reverse_geocode(df['latitude'][0], df['longitude'][0])
print(res.country)

However, when I try to get country for all columns it won't work:

df['country'] = (Geocoder.reverse_geocode(df['latitude'], df['longitude'])).country

"KeyError = 12"

I have not found anything online with a solution I can get to work. Any suggestions?

241095
  • 1
  • 1
  • 1
    the func probably doesn't understand `Series`, try `apply`: `df['country'] = df.apply(lambda x: Geocoder.reverse_geocode(x['latitude'], x['longitude']).country, axis=1)` – EdChum Feb 28 '17 at 17:04

1 Answers1

0

IIUC it's likely that reverse_geocode doesn't understand Series args try apply to pass each row value in turn:

df['country'] = df.apply(lambda x: Geocoder.reverse_geocode(x['latitude'], x['longitude']).country, axis=1)
EdChum
  • 376,765
  • 198
  • 813
  • 562
  • Doesn't seem to work with dataframe as large as mine. – 241095 Feb 28 '17 at 17:47
  • 1
    doesn't work is not a complete explanation, post raw data, your code, and what 'doesn't seem to work' actually means – EdChum Feb 28 '17 at 17:48
  • Sorry. It means when I take a smaller version of the dataframe (c.a. 50 rows), it takes a little time but works perfectly. On my actual dataframe (thousands of rows) it takes forever, then returns query over limit response. – 241095 Feb 28 '17 at 18:01
  • 1
    That is a library limitation I believe nothing to do with this code. I think you're limited in the number of requests, that's a separate issue to your original problem – EdChum Feb 28 '17 at 18:24
  • which library are you using for reverse_geocoding ? Like @EdChum I think you're limited by the number of requests, so maybe you could use a library that enables offline queries such as this one : https://github.com/thampiman/reverse-geocoder – arthur Mar 01 '17 at 09:45