0

I have thousands of address in my dataframe, I was cry paste for one-by-one longitude and latitude in spreadsheet, but I want to automate this in thousands of address. Here's my previous code

from geopy.geocoders import GoogleV3
geolocator = GoogleV3(api_key= "Code")
location = geolocator.geocode("JL.Balitung III")
print((location.latitude, location.longitude))

This is the output

(-6.232012999999999, 106.8087392)

Here's my data

id  Address 
1   JL.Balitung III
2   Jalan Balitung III
3   Jl Balitung III

Here's what I want

id  Address                  Latitude               Longitude        
1   JL.Balitung III          -6.232012999999999     106.8087392
2   Jalan Erlangga III       -6.2336678             106.8086881
3   Jalan Senopati           -6.2302063             106.8066563
joris
  • 133,120
  • 36
  • 247
  • 202
Nabih Bawazir
  • 6,381
  • 7
  • 37
  • 70

1 Answers1

2

Use custom function and assign to new columns:

from geopy.geocoders import GoogleV3
geolocator = GoogleV3(api_key= "code")

def f(x):
    location = geolocator.geocode(x)
    return pd.Series((location.latitude, location.longitude))

df[['Latitude','Longitude']] = df['Address'].apply(f)
print (df)
   id             Address  Latitude   Longitude
0   1     JL.Balitung III -6.232013  106.808739
1   2  Jalan Erlangga III -6.233668  106.808688
2   3      Jalan Senopati -6.230206  106.806656
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252