recode_region
recode_region(x, which='region', quiet=False)Look up the region or subregion for a country.
Vectorised lookup from country name to world region or finer subregion, using the bundled country_region table. Matching is case-insensitive and whitespace-tolerant, and understands the spellings people type as well as ISO 3166-1 alpha-2 and alpha-3 codes, so “USA”, “U.S.”, “us” and “United States” all resolve alike; blanks and non-answers (see na_blank()) become missing, and unmatched countries become missing with a one-line warning so spelling mismatches are easy to spot.
A country column that people typed themselves is rarely tidy: in one real open salary survey, nine thousand respondents wrote “United States” and eight thousand wrote “USA”. So the name is folded down before it is looked up (case, stray punctuation, accents and a leading “the” are all dropped) and then tried against the bundled table, then against a table of the spellings people actually use: endonyms (“Deutschland”, “Brasil”), the constituent countries of the United Kingdom, and ISO 3166-1 alpha-2 and alpha-3 codes (“US”, “USA”, “DE”, “DEU”).
A two-letter code is only read as a code when it was typed in capitals, because “NO” is Norway but “no” is an answer to a different question. Anything still unmatched returns missing with a warning listing the offenders. region is the coarse level (e.g. “Europe”); subregion is finer (e.g. “Western Europe”).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| x | list or Series | Country names. | required |
| which | str | “region” (default) or “subregion”. | 'region' |
| quiet | bool | If False (default), warn about unmatched countries. | False |
Returns
| Name | Type | Description |
|---|---|---|
| pandas.Series | Regions (or subregions), missing where unmatched. |
See Also
add_region, country_region
Examples
>>> recode_region(["Germany", "Japan", "Brazil"])
>>> recode_region(["USA", "U.S.", "England", "Deutschland", "Holland"])
>>> recode_region(["US", "GBR", "DE", "JPN"])
>>> recode_subregion(["Germany", "Japan"])
>>> recode_region(["Germany", "Atlantis"], quiet=True)