link(df, scale, on, key='auto', errors='raise', ref=None)
Attach geo_id to a table that identifies units by name or by a source code.
Parameters
| df |
pd.DataFrame |
Your table (one row per unit, or per unit and year) |
required |
| scale |
str |
Scale of your units, e.g. "province" (see geo.scales()) |
required |
| on |
str |
The column of df that identifies the unit |
required |
| key |
str |
The column of geo.units(scale) to match against: "name", a source code column of geo.units(scale) (e.g. "gid" for GADM codes), or "auto": the column matching most values, with the others filling the gaps (so mixed spellings from different sources still link). Matching ignores accents, case, spaces and punctuation (“BântéayMéanchey” = “Banteay Meanchey”). A value shared by several units (two municipalities with the same name) is ambiguous and links to none of them: link such units by a code instead. |
'auto' |
| errors |
str |
"raise" (default), "warn" or "ignore" for values that match no unit or are ambiguous |
'raise' |
Returns
|
pandas.DataFrame |
A copy of df with geo_id as its first column. |
Example
!pip install -q geo4bolivia
import geo4bolivia as geo
import plotly.express as px
import pandas as pd
mine = pd.DataFrame({"municipio": ["SUCRE", "el alto", "Cobija"],
"clinics": [41, 63, 12]}) # made-up values
mine = geo.link(mine, scale="municipality", on="municipio")
by_code = geo.link(pd.DataFrame({"cod": [20101]}), scale="municipality",
on="cod", key="mun_id") # INE code -> geo_id
geo.add_names(mine)
| 0 |
BO-01-06-02 |
Sucre |
SUCRE |
41 |
| 1 |
BO-04-17-02 |
El Alto |
el alto |
63 |
| 2 |
BO-06-05-03 |
Cobija |
Cobija |
12 |