import pandas as pd
import ezrsurvey as ezCountries, regions and currency
Two columns make a multi-country study harder than a single-market one. The country question is usually free text, so it comes back holding every spelling a person can produce. And if respondents answered about money in their own currency, the amounts column cannot be averaged at all.
The country column people typed
On one real open salary survey of 26,232 people, nine thousand wrote “United States”, eight thousand wrote “USA”, two and a half thousand wrote “US”, and several hundred more wrote “U.S.”, “Usa” or “usa”. Matching on the name as typed resolved about half the column.
recode_region() folds the answer down before looking it up. Case, punctuation, accents and a leading “the” all stop mattering, and then a table of the spellings people actually use is tried.
ez.recode_region(["United States", "USA", "U.S.", "Estados Unidos"])0 North America
1 North America
2 North America
3 North America
dtype: object
It carries the constituent countries of the United Kingdom, which a UK respondent is as likely to write as the country itself:
ez.recode_region(["England", "Scotland", "Wales", "Northern Ireland"])0 Europe
1 Europe
2 Europe
3 Europe
dtype: object
Endonyms, because a survey fielded in more than one language collects them:
ez.recode_region(["Deutschland", "Brasil", "Espana", "Osterreich"])0 Europe
1 South America
2 Europe
3 Europe
dtype: object
And the official ISO 3166-1 names, which is what a questionnaire built on the standard puts in front of a respondent, in either word order:
ez.recode_region(["Viet Nam", "Syrian Arab Republic", "Republic of Moldova", "Cabo Verde", "Eswatini"])0 Asia
1 Middle East
2 Europe
3 Africa
4 Africa
dtype: object
The fold is done letter by letter rather than left to the platform’s transliteration, so a name carrying an accent matches the same way on Windows, macOS and Linux:
ez.recode_region(["Côte d'Ivoire", "Réunion", "Türkiye"])0 Africa
1 Africa
2 Europe
dtype: object
Adding the column
add_region() does the same thing to a data frame, and subregion=True adds the finer grouping alongside:
survey = ez.add_region(ez.podracing_survey, "demo_country", subregion=True)
ez.calc_percentage(survey, "region", sort="desc")| region | n | pct | |
|---|---|---|---|
| 0 | North America | 503 | 50.0 |
| 1 | Europe | 314 | 31.0 |
| 2 | South America | 82 | 8.0 |
| 3 | Asia | 59 | 6.0 |
| 4 | Oceania | 42 | 4.0 |
ez.calc_percentage(survey, "subregion", sort="desc")| subregion | n | pct | |
|---|---|---|---|
| 0 | North America | 503 | 50.0 |
| 1 | Western Europe | 286 | 29.0 |
| 2 | South America | 82 | 8.0 |
| 3 | East Asia | 59 | 6.0 |
| 4 | Australia New Zealand | 42 | 4.0 |
| 5 | Northern Europe | 28 | 3.0 |
Anything that did not match comes back missing with a warning naming the values, because at that point what is left is genuine typos worth seeing rather than spellings worth supporting.
Reporting against the standard
A client who consolidates research across agencies wants countries coded to ISO 3166-1, not to whatever names this analyst happened to pick. A column already coded that way needs no preparation:
ez.recode_region(["US", "GBR", "DE", "JPN", "AUS"])0 North America
1 Europe
2 Europe
3 Asia
4 Oceania
dtype: object
A two-letter code that is also an ordinary English word is only read as a country when it was typed in capitals, so "NO" is Norway and "no" is somebody answering a different question:
ez.recode_region(["NO", "no"], quiet=True)0 Europe
1 NaN
dtype: object
The bundled table carries both codes, so a country name can be replaced by its code on the way out. That is the table to join to, because country names differ between sources and codes do not:
ez.country_region.head()| country | iso2 | iso3 | region | subregion | |
|---|---|---|---|---|---|
| 0 | Algeria | DZ | DZA | Africa | Africa |
| 1 | Angola | AO | AGO | Africa | Africa |
| 2 | Benin | BJ | BEN | Africa | Africa |
| 3 | Botswana | BW | BWA | Africa | Africa |
| 4 | Burkina Faso | BF | BFA | Africa | Africa |
codes = ez.country_region[["country", "iso2", "iso3"]]
coded = survey.merge(codes, left_on="demo_country", right_on="country", how="left")
coded.groupby(["iso3", "region"]).size().sort_values(ascending=False).head(8)iso3 region
USA North America 336
GBR Europe 120
MEX North America 93
DEU Europe 89
BRA South America 82
FRA Europe 77
CAN North America 74
JPN Asia 59
dtype: int64
Namibia’s alpha-2 code is the two letters
NA, not a missing value. It is worth knowing before filtering that column, and before reading a country file with pandas, which treats"NA"as missing unless toldkeep_default_na=False. The bundled table already keeps it.
The regions themselves follow the UN M49 sub-regions with two deliberate departures, because they are how consumer research buys these markets: the Middle East is a region of its own rather than part of Asia and Africa, and the Americas are split north and south.
Money in eleven currencies
A single amount converts with convert_currency(). The source currency is from_, because from is a reserved word in Python:
ez.convert_currency(50000, from_="GBP", to="USD")0 63291.139241
dtype: float64
ez.convert_currency([50000, 60000], from_=["EUR", "CAD"], to="USD")0 54347.826087
1 44117.647059
dtype: float64
A whole column converts with add_currency(), which takes the currency from a second column, so each row is converted at its own rate:
pay = pd.DataFrame({
"respondent": [1, 2, 3, 4, 5],
"salary": [52000, 61000, 48000, 75000, 39000],
"currency": ["GBP", "EUR", "USD", "CAD", "AUD"],
})
ez.add_currency(pay, "salary", from_="currency", to="EUR", into="salary_eur")| respondent | salary | currency | salary_eur | |
|---|---|---|---|---|
| 0 | 1 | 52000 | GBP | 60556.962025 |
| 1 | 2 | 61000 | EUR | 61000.000000 |
| 2 | 3 | 48000 | USD | 44160.000000 |
| 3 | 4 | 75000 | CAD | 50735.294118 |
| 4 | 5 | 39000 | AUD | 23605.263158 |
The rates are bundled rather than fetched, so a figure computed today is the same figure next year. That is what a published number needs, and it is also why they are a dated snapshot rather than live rates.
ez.list_currencies()[:6]['USD', 'EUR', 'GBP', 'JPY', 'CNY', 'CHF']
When accuracy matters more than reproducibility, pass your own:
own_rates = {"USD": 1, "GBP": 0.79, "EUR": 0.92}
ez.convert_currency(50000, from_="GBP", to="EUR", rates=own_rates)0 58227.848101
dtype: float64
A code the table does not hold is reported rather than guessed at. Real currency columns contain things like "AUD/NZD" and "Other", and those rows come back missing with a warning naming the codes.