Глядя на api docs , вы хотите одну строку, представляющую адрес из ваших столбцов отдельных компонентов адреса, например:
location = client.geocode("1109 N Highland St, Arlington VA")
Таким образом, чтобы получить такой столбец вВ df
вы можете отобразить каждый вектор в строку, а затем использовать простую конкатенацию строк, чтобы создать единственную строку, которая затем будет вставлена в новую серию в вашем 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)
Производить:
# >>> 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
Затем вы можете перебирать значения Строк адресных строк и сохранять точность в списке, который можно вставить в ваш 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']]
Результаты df
будет выглядеть следующим образом:
# >>> 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
Затем, чтобы записать результаты df
в .csv
:
results.to_csv('results.csv')
Объединение всего этого приводит к следующему коду:
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')