-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeocoding.py
More file actions
20 lines (17 loc) · 802 Bytes
/
geocoding.py
File metadata and controls
20 lines (17 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from decouple import config
import pandas as pd
import googlemaps
class GoogleGeocoding:
def __init__(self):
self.key = config('api_key')
self.gmaps = googlemaps.Client(key=self.key)
def geocode_df(self, dataframe):
print('Preparing for geocoding country code...')
df = dataframe
df = df.value_counts().rename_axis('country').reset_index(name='counts')
for index in df.index:
df.loc[index, 'longitude'] = (self.gmaps.geocode(df['country'][index]))[0].get('geometry').get('location').get('lng')
df.loc[index, 'latitude'] = (self.gmaps.geocode(df['country'][index]))[0].get('geometry').get('location').get('lat')
df.to_csv('geocode_data.csv', index=False)
print('Geocoding completed')
return df