{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Geocode a DataFrame\n",
"\n",
"This example illustrates how simply use the Geocoding Data Service.\n",
"\n",
"_Note: You'll need [CARTO Account](https://carto.com/signup) credentials to reproduce this example._"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from cartoframes.auth import set_default_credentials\n",
"\n",
"set_default_credentials('creds.json')"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" address | \n",
" city | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Gran Vía 46 | \n",
" Madrid | \n",
"
\n",
" \n",
" 1 | \n",
" Ebro 1 | \n",
" Sevilla | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" address city\n",
"0 Gran Vía 46 Madrid\n",
"1 Ebro 1 Sevilla"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from pandas import DataFrame\n",
"\n",
"df = DataFrame([['Gran Vía 46', 'Madrid'], ['Ebro 1', 'Sevilla']], columns=['address','city'])\n",
"df"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Success! Data geocoded correctly\n"
]
}
],
"source": [
"from cartoframes.data.services import Geocoding\n",
"\n",
"gc = Geocoding()\n",
"gdf, metadata = gc.geocode(df, street='address', city='city', country={'value': 'Spain'})"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" the_geom | \n",
" address | \n",
" city | \n",
" gc_status_rel | \n",
" carto_geocode_hash | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" POINT (-3.70588 40.42049) | \n",
" Gran Vía 46 | \n",
" Madrid | \n",
" 0.84 | \n",
" 95e4f39284efeab8e759aaa547d84567 | \n",
"
\n",
" \n",
" 1 | \n",
" POINT (-5.98312 37.35547) | \n",
" Ebro 1 | \n",
" Sevilla | \n",
" 0.70 | \n",
" 66940c4beeb395e1b628587ac772763a | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" the_geom address city gc_status_rel \\\n",
"0 POINT (-3.70588 40.42049) Gran Vía 46 Madrid 0.84 \n",
"1 POINT (-5.98312 37.35547) Ebro 1 Sevilla 0.70 \n",
"\n",
" carto_geocode_hash \n",
"0 95e4f39284efeab8e759aaa547d84567 \n",
"1 66940c4beeb395e1b628587ac772763a "
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gdf"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'total_rows': 2,\n",
" 'required_quota': 2,\n",
" 'previously_geocoded': 0,\n",
" 'previously_failed': 0,\n",
" 'records_with_geometry': 0,\n",
" 'final_records_with_geometry': 2,\n",
" 'geocoded_increment': 2,\n",
" 'successfully_geocoded': 2,\n",
" 'failed_geocodings': 0}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"metadata"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
""
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from cartoframes.viz import Layer\n",
"\n",
"Layer(gdf)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}