Skip to content

Commit

Permalink
Optimize the country check
Browse files Browse the repository at this point in the history
  • Loading branch information
petya-vasileva committed Aug 3, 2024
1 parent ac6638d commit c253b18
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion data_objects/MetaData.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ def get_country(lat, lon):
location = geolocator.reverse((lat, lon), exactly_one=True, language='en')
return location.raw['address']['country']

df['country'] = df.apply(lambda row: get_country(row['lat'], row['lon']), axis=1)
unique_lat_lon = df[['lat', 'lon']].drop_duplicates()
unique_lat_lon['country'] = unique_lat_lon.apply(lambda row: get_country(row['lat'], row['lon']), axis=1)
lat_lon_to_country = dict(zip(unique_lat_lon.set_index(['lat', 'lon']).index, unique_lat_lon['country']))

df['country'] = df.apply(lambda row: lat_lon_to_country.get((row['lat'], row['lon'])), axis=1)

return df

Expand Down

0 comments on commit c253b18

Please sign in to comment.