Looking at the api docs you want a single string representing the address from your columns of individual address components like the following:
location = client.geocode("1109 N Highland St, Arlington VA")
So to get a column like that in your df
you could map each vector to a string and then use simple string concatenation to produce a single string that is then inserted into a new series in your df
:
import pandas as pd
customers = pd.read_csv("example.csv", header=None)
customers['address_string'] = customers[0].map(str) + ' ' + customers[1].map(str) + customers[2].map(str)
Producing:
# >>> customers['address_string']
# 0 21236 Birchwood Loop 99567 AK
# 1 1731 Bragaw St 99508 AK
# 2 300 E Fireweed Ln 99503 AK
# 3 4360 Snider Dr 99654 AK
# 4 1921 W Dimond Blvd 108 99515 AK
Then you can iterate over the values of the Series of address strings and store the accuracy in a list that can be inserted into your df
:
geocoded_acuracy = []
geocoded_acuracy_type = []
for address in customers['address_string'].values:
geocoded_address = client.geocode(address)
accuracy = geocoded_address.best_match.get("accuracy")
accuracy_type = geocoded_address.best_match.get("accuracy_type")
geocoded_acuracy.append(accuracy)
geocoded_acuracy_type.append(accuracy_type)
customers['accuracy'] = geocoded_acuracy
customers['accuracy_type'] = geocoded_acuracy_type
results = customers[['address_string', 'accuracy', 'accuracy_type']]
The results df
would then look like the following:
# >>> results
# address_string accuracy accuracy_type
# 0 21236 Birchwood Loop 99567 AK 1.00 rooftop
# 1 1731 Bragaw St 99508 AK 1.00 rooftop
# 2 300 E Fireweed Ln 99503 AK 1.00 rooftop
# 3 4360 Snider Dr 99654 AK 1.00 range_interpolation
# 4 1921 W Dimond Blvd 108 99515 AK 1.00 rooftop
# 5 2702 Peger Rd 99709 AK 1.00 rooftop
# 6 1651 College Rd 99709 AK 1.00 rooftop
# 7 898 Ballaine Rd 99709 AK 1.00 rooftop
# 8 23819 Immelman Circle 99567 AK 1.00 rooftop
# 9 9750 W Parks Hwy 99652 AK 0.33 place
# 10 7205 Shorewood Dr 99645 AK 1.00 range_interpolation
Then to write the results df
to a .csv
:
results.to_csv('results.csv')
Putting all of this together yields the following code:
import pandas as pd
from geocodio import GeocodioClient
API_KEY = 'insert_your_key_here'
client = GeocodioClient(API_KEY)
customers = pd.read_csv("example.csv", header=None)
customers['address_string'] = customers[0].map(str) + ' ' + customers[1].map(str) + customers[2].map(str)
geocoded_acuracy = []
geocoded_acuracy_type = []
for address in customers['address_string'].values:
geocoded_address = client.geocode(address)
accuracy = geocoded_address.best_match.get("accuracy")
accuracy_type = geocoded_address.best_match.get("accuracy_type")
geocoded_acuracy.append(accuracy)
geocoded_acuracy_type.append(accuracy_type)
customers['accuracy'] = geocoded_acuracy
customers['accuracy_type'] = geocoded_acuracy_type
results = customers[['address_string', 'accuracy', 'accuracy_type']]
results.to_csv('results.csv')