Note
Go to the end to download the full example code or to run this example in your browser via JupyterLite or Binder.
Fuzzy joining dirty tables with the Joiner#
Here we show how to combine data from different sources, with a vocabulary not well normalized.
Joining is difficult: one entry on one side does not have an exact match on the other side.
The fuzzy_join() function enables to join tables without cleaning the data by
accounting for the label variations.
To illustrate, we will join data from the 2022 World Happiness Report, with tables provided in the World Bank open data platform in order to create a first prediction model.
Moreover, the Joiner() is a scikit-learn Transformer that makes it easy to
use such fuzzy joining multiple tables to bring in information in a
machine-learning pipeline. In particular, it enables tuning parameters of
fuzzy_join() to find the matches that maximize prediction accuracy.
Data Importing and preprocessing#
We import the happiness score table first:
import pandas as pd
from skrub import datasets
happiness_data = datasets.fetch_country_happiness()
df = pd.read_csv(happiness_data.happiness_report_path)
Let’s look at the table:
df.head(3)
| RANK | Country | Happiness score | Whisker-high | Whisker-low | Dystopia (1.83) + residual | Explained by: GDP per capita | Explained by: Social support | Explained by: Healthy life expectancy | Explained by: Freedom to make life choices | Explained by: Generosity | Explained by: Perceptions of corruption | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | Finland | 7.82e+03 | 7.89e+03 | 7.76e+03 | 2.52e+03 | 1.89e+03 | 1.26e+03 | 775. | 736. | 109. | 534. |
| 1 | 2 | Denmark | 7.64e+03 | 7.71e+03 | 7.56e+03 | 2.23e+03 | 1.95e+03 | 1.24e+03 | 777. | 719. | 188. | 532. |
| 2 | 3 | Iceland | 7.56e+03 | 7.65e+03 | 7.46e+03 | 2.32e+03 | 1.94e+03 | 1.32e+03 | 803. | 718. | 270. | 191. |
RANK
Int64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 2.00 ± 1.00
- Median ± IQR
- 2 ± 2
- Min | Max
- 1 | 3
Country
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
Most frequent values
Finland
Denmark
Iceland
['Finland', 'Denmark', 'Iceland']
Happiness score
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 7.67e+03 ± 136.
- Median ± IQR
- 7.64e+03 ± 264.
- Min | Max
- 7.56e+03 | 7.82e+03
Whisker-high
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 7.75e+03 ± 122.
- Median ± IQR
- 7.71e+03 ± 235.
- Min | Max
- 7.65e+03 | 7.89e+03
Whisker-low
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 7.59e+03 ± 149.
- Median ± IQR
- 7.56e+03 ± 292.
- Min | Max
- 7.46e+03 | 7.76e+03
Dystopia (1.83) + residual
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 2.35e+03 ± 149.
- Median ± IQR
- 2.32e+03 ± 292.
- Min | Max
- 2.23e+03 | 2.52e+03
Explained by: GDP per capita
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 1.93e+03 ± 31.5
- Median ± IQR
- 1.94e+03 ± 61.0
- Min | Max
- 1.89e+03 | 1.95e+03
Explained by: Social support
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 1.27e+03 ± 40.8
- Median ± IQR
- 1.26e+03 ± 77.0
- Min | Max
- 1.24e+03 | 1.32e+03
Explained by: Healthy life expectancy
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 785. ± 15.6
- Median ± IQR
- 777. ± 28.0
- Min | Max
- 775. | 803.
Explained by: Freedom to make life choices
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 724. ± 10.1
- Median ± IQR
- 719. ± 18.0
- Min | Max
- 718. | 736.
Explained by: Generosity
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 189. ± 80.5
- Median ± IQR
- 188. ± 161.
- Min | Max
- 109. | 270.
Explained by: Perceptions of corruption
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 419. ± 197.
- Median ± IQR
- 532. ± 343.
- Min | Max
- 191. | 534.
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
|
Column
|
Column name
|
dtype
|
Is sorted
|
Null values
|
Unique values
|
Mean
|
Std
|
Min
|
Median
|
Max
|
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | RANK | Int64DType | True | 0 (0.0%) | 3 (100.0%) | 2.00 | 1.00 | 1 | 2 | 3 |
| 1 | Country | StringDtype | False | 0 (0.0%) | 3 (100.0%) | |||||
| 2 | Happiness score | Float64DType | True | 0 (0.0%) | 3 (100.0%) | 7.67e+03 | 136. | 7.56e+03 | 7.64e+03 | 7.82e+03 |
| 3 | Whisker-high | Float64DType | True | 0 (0.0%) | 3 (100.0%) | 7.75e+03 | 122. | 7.65e+03 | 7.71e+03 | 7.89e+03 |
| 4 | Whisker-low | Float64DType | True | 0 (0.0%) | 3 (100.0%) | 7.59e+03 | 149. | 7.46e+03 | 7.56e+03 | 7.76e+03 |
| 5 | Dystopia (1.83) + residual | Float64DType | False | 0 (0.0%) | 3 (100.0%) | 2.35e+03 | 149. | 2.23e+03 | 2.32e+03 | 2.52e+03 |
| 6 | Explained by: GDP per capita | Float64DType | False | 0 (0.0%) | 3 (100.0%) | 1.93e+03 | 31.5 | 1.89e+03 | 1.94e+03 | 1.95e+03 |
| 7 | Explained by: Social support | Float64DType | False | 0 (0.0%) | 3 (100.0%) | 1.27e+03 | 40.8 | 1.24e+03 | 1.26e+03 | 1.32e+03 |
| 8 | Explained by: Healthy life expectancy | Float64DType | True | 0 (0.0%) | 3 (100.0%) | 785. | 15.6 | 775. | 777. | 803. |
| 9 | Explained by: Freedom to make life choices | Float64DType | True | 0 (0.0%) | 3 (100.0%) | 724. | 10.1 | 718. | 719. | 736. |
| 10 | Explained by: Generosity | Float64DType | True | 0 (0.0%) | 3 (100.0%) | 189. | 80.5 | 109. | 188. | 270. |
| 11 | Explained by: Perceptions of corruption | Float64DType | True | 0 (0.0%) | 3 (100.0%) | 419. | 197. | 191. | 532. | 534. |
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
RANK
Int64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 2.00 ± 1.00
- Median ± IQR
- 2 ± 2
- Min | Max
- 1 | 3
Country
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
Most frequent values
Finland
Denmark
Iceland
['Finland', 'Denmark', 'Iceland']
Happiness score
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 7.67e+03 ± 136.
- Median ± IQR
- 7.64e+03 ± 264.
- Min | Max
- 7.56e+03 | 7.82e+03
Whisker-high
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 7.75e+03 ± 122.
- Median ± IQR
- 7.71e+03 ± 235.
- Min | Max
- 7.65e+03 | 7.89e+03
Whisker-low
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 7.59e+03 ± 149.
- Median ± IQR
- 7.56e+03 ± 292.
- Min | Max
- 7.46e+03 | 7.76e+03
Dystopia (1.83) + residual
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 2.35e+03 ± 149.
- Median ± IQR
- 2.32e+03 ± 292.
- Min | Max
- 2.23e+03 | 2.52e+03
Explained by: GDP per capita
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 1.93e+03 ± 31.5
- Median ± IQR
- 1.94e+03 ± 61.0
- Min | Max
- 1.89e+03 | 1.95e+03
Explained by: Social support
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 1.27e+03 ± 40.8
- Median ± IQR
- 1.26e+03 ± 77.0
- Min | Max
- 1.24e+03 | 1.32e+03
Explained by: Healthy life expectancy
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 785. ± 15.6
- Median ± IQR
- 777. ± 28.0
- Min | Max
- 775. | 803.
Explained by: Freedom to make life choices
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 724. ± 10.1
- Median ± IQR
- 719. ± 18.0
- Min | Max
- 718. | 736.
Explained by: Generosity
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 189. ± 80.5
- Median ± IQR
- 188. ± 161.
- Min | Max
- 109. | 270.
Explained by: Perceptions of corruption
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 419. ± 197.
- Median ± IQR
- 532. ± 343.
- Min | Max
- 191. | 534.
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
| Column 1 | Column 2 | Cramér's V | Pearson's Correlation |
|---|---|---|---|
| Explained by: Generosity | Explained by: Perceptions of corruption | 1.00 | -0.874 |
| Explained by: Freedom to make life choices | Explained by: Perceptions of corruption | 1.00 | 0.546 |
| Explained by: Freedom to make life choices | Explained by: Generosity | 1.00 | -0.885 |
| Explained by: Healthy life expectancy | Explained by: Perceptions of corruption | 1.00 | -0.998 |
| Explained by: Healthy life expectancy | Explained by: Generosity | 1.00 | 0.901 |
| Explained by: Healthy life expectancy | Explained by: Freedom to make life choices | 1.00 | -0.595 |
| Explained by: Social support | Explained by: Perceptions of corruption | 1.00 | -0.982 |
| Explained by: Social support | Explained by: Generosity | 1.00 | 0.766 |
| Explained by: Social support | Explained by: Freedom to make life choices | 1.00 | -0.379 |
| Explained by: Social support | Explained by: Healthy life expectancy | 1.00 | 0.969 |
| Explained by: GDP per capita | Explained by: Perceptions of corruption | 1.00 | -0.252 |
| Explained by: GDP per capita | Explained by: Generosity | 1.00 | 0.691 |
| Explained by: GDP per capita | Explained by: Freedom to make life choices | 1.00 | -0.948 |
| Explained by: GDP per capita | Explained by: Healthy life expectancy | 1.00 | 0.309 |
| Explained by: GDP per capita | Explained by: Social support | 1.00 | 0.0654 |
| Dystopia (1.83) + residual | Explained by: Perceptions of corruption | 1.00 | 0.206 |
| Dystopia (1.83) + residual | Explained by: Generosity | 1.00 | -0.656 |
| Dystopia (1.83) + residual | Explained by: Freedom to make life choices | 1.00 | 0.932 |
| Dystopia (1.83) + residual | Explained by: Healthy life expectancy | 1.00 | -0.264 |
| Dystopia (1.83) + residual | Explained by: Social support | 1.00 | -0.0180 |
| Dystopia (1.83) + residual | Explained by: GDP per capita | 1.00 | -0.999 |
| Whisker-low | Explained by: Perceptions of corruption | 1.00 | 0.763 |
| Whisker-low | Explained by: Generosity | 1.00 | -0.981 |
| Whisker-low | Explained by: Freedom to make life choices | 1.00 | 0.958 |
| Whisker-low | Explained by: Healthy life expectancy | 1.00 | -0.800 |
| Whisker-low | Explained by: Social support | 1.00 | -0.628 |
| Whisker-low | Explained by: GDP per capita | 1.00 | -0.818 |
| Whisker-low | Dystopia (1.83) + residual | 1.00 | 0.790 |
| Whisker-high | Explained by: Perceptions of corruption | 1.00 | 0.698 |
| Whisker-high | Explained by: Generosity | 1.00 | -0.958 |
| Whisker-high | Explained by: Freedom to make life choices | 1.00 | 0.981 |
| Whisker-high | Explained by: Healthy life expectancy | 1.00 | -0.739 |
| Whisker-high | Explained by: Social support | 1.00 | -0.550 |
| Whisker-high | Explained by: GDP per capita | 1.00 | -0.869 |
| Whisker-high | Dystopia (1.83) + residual | 1.00 | 0.845 |
| Whisker-high | Whisker-low | 1.00 | 0.995 |
| Happiness score | Explained by: Perceptions of corruption | 1.00 | 0.734 |
| Happiness score | Explained by: Generosity | 1.00 | -0.972 |
| Happiness score | Explained by: Freedom to make life choices | 1.00 | 0.970 |
| Happiness score | Explained by: Healthy life expectancy | 1.00 | -0.773 |
| Happiness score | Explained by: Social support | 1.00 | -0.593 |
| Happiness score | Explained by: GDP per capita | 1.00 | -0.842 |
| Happiness score | Dystopia (1.83) + residual | 1.00 | 0.816 |
| Happiness score | Whisker-low | 1.00 | 0.999 |
| Happiness score | Whisker-high | 1.00 | 0.999 |
| Country | Explained by: Perceptions of corruption | 1.00 | |
| Country | Explained by: Generosity | 1.00 | |
| Country | Explained by: Freedom to make life choices | 1.00 | |
| Country | Explained by: Healthy life expectancy | 1.00 | |
| Country | Explained by: Social support | 1.00 | |
| Country | Explained by: GDP per capita | 1.00 | |
| Country | Dystopia (1.83) + residual | 1.00 | |
| Country | Whisker-low | 1.00 | |
| Country | Whisker-high | 1.00 | |
| Country | Happiness score | 1.00 | |
| RANK | Explained by: Perceptions of corruption | 1.00 | -0.869 |
| RANK | Explained by: Generosity | 1.00 | 1.00 |
| RANK | Explained by: Freedom to make life choices | 1.00 | -0.890 |
| RANK | Explained by: Healthy life expectancy | 1.00 | 0.896 |
| RANK | Explained by: Social support | 1.00 | 0.759 |
| RANK | Explained by: GDP per capita | 1.00 | 0.699 |
| RANK | Dystopia (1.83) + residual | 1.00 | -0.664 |
| RANK | Whisker-low | 1.00 | -0.983 |
| RANK | Whisker-high | 1.00 | -0.961 |
| RANK | Happiness score | 1.00 | -0.974 |
| RANK | Country | 1.00 |
Please enable javascript
The skrub table reports need javascript to display correctly. If you are displaying a report in a Jupyter notebook and you see this message, you may need to re-execute the cell or to trust the notebook (button on the top right or "File > Trust notebook").
This is a table that contains the happiness index of a country along with some of the possible explanatory factors: GDP per capita, Social support, Generosity etc.
For the sake of this example, we only keep the country names and our variable of interest: the ‘Happiness score’.
Additional tables from other sources#
Now, we need to include explanatory factors from other sources, to complete our covariates (X table).
Interesting tables can be found on the World Bank open data platform, which are also available in the dataset We extract the table containing GDP per capita by country:
| Country Name | GDP per capita (current US$) | |
|---|---|---|
| 0 | Aruba | 2.93e+04 |
| 1 | Africa Eastern and Southern | 1.62e+03 |
| 2 | Afghanistan | 364. |
Country Name
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
Most frequent values
Aruba
Africa Eastern and Southern
Afghanistan
['Aruba', 'Africa Eastern and Southern', 'Afghanistan']
GDP per capita (current US$)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 1.04e+04 ± 1.64e+04
- Median ± IQR
- 1.62e+03 ± 2.90e+04
- Min | Max
- 364. | 2.93e+04
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
|
Column
|
Column name
|
dtype
|
Is sorted
|
Null values
|
Unique values
|
Mean
|
Std
|
Min
|
Median
|
Max
|
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Country Name | StringDtype | True | 0 (0.0%) | 3 (100.0%) | |||||
| 1 | GDP per capita (current US$) | Float64DType | True | 0 (0.0%) | 3 (100.0%) | 1.04e+04 | 1.64e+04 | 364. | 1.62e+03 | 2.93e+04 |
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
Country Name
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
Most frequent values
Aruba
Africa Eastern and Southern
Afghanistan
['Aruba', 'Africa Eastern and Southern', 'Afghanistan']
GDP per capita (current US$)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 1.04e+04 ± 1.64e+04
- Median ± IQR
- 1.62e+03 ± 2.90e+04
- Min | Max
- 364. | 2.93e+04
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
| Column 1 | Column 2 | Cramér's V | Pearson's Correlation |
|---|---|---|---|
| Country Name | GDP per capita (current US$) | 1.00 |
Please enable javascript
The skrub table reports need javascript to display correctly. If you are displaying a report in a Jupyter notebook and you see this message, you may need to re-execute the cell or to trust the notebook (button on the top right or "File > Trust notebook").
Then another table, with life expectancy by country:
| Country Name | Life expectancy at birth, total (years) | |
|---|---|---|
| 0 | Aruba | 74.6 |
| 1 | Africa Eastern and Southern | 62.5 |
| 2 | Afghanistan | 62.0 |
Country Name
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
Most frequent values
Aruba
Africa Eastern and Southern
Afghanistan
['Aruba', 'Africa Eastern and Southern', 'Afghanistan']
Life expectancy at birth, total (years)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 66.4 ± 7.17
- Median ± IQR
- 62.5 ± 12.6
- Min | Max
- 62.0 | 74.6
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
|
Column
|
Column name
|
dtype
|
Is sorted
|
Null values
|
Unique values
|
Mean
|
Std
|
Min
|
Median
|
Max
|
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Country Name | StringDtype | True | 0 (0.0%) | 3 (100.0%) | |||||
| 1 | Life expectancy at birth, total (years) | Float64DType | True | 0 (0.0%) | 3 (100.0%) | 66.4 | 7.17 | 62.0 | 62.5 | 74.6 |
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
Country Name
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
Most frequent values
Aruba
Africa Eastern and Southern
Afghanistan
['Aruba', 'Africa Eastern and Southern', 'Afghanistan']
Life expectancy at birth, total (years)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 66.4 ± 7.17
- Median ± IQR
- 62.5 ± 12.6
- Min | Max
- 62.0 | 74.6
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
| Column 1 | Column 2 | Cramér's V | Pearson's Correlation |
|---|---|---|---|
| Country Name | Life expectancy at birth, total (years) | 1.00 |
Please enable javascript
The skrub table reports need javascript to display correctly. If you are displaying a report in a Jupyter notebook and you see this message, you may need to re-execute the cell or to trust the notebook (button on the top right or "File > Trust notebook").
And a table with legal rights strength by country:
| Country Name | Strength of legal rights index (0=weak to 12=strong) | |
|---|---|---|
| 0 | Africa Eastern and Southern | 4.54 |
| 1 | Afghanistan | 10.0 |
| 2 | Africa Western and Central | 5.86 |
Country Name
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
Most frequent values
Africa Eastern and Southern
Afghanistan
Africa Western and Central
['Africa Eastern and Southern', 'Afghanistan', 'Africa Western and Central']
Strength of legal rights index (0=weak to 12=strong)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 6.80 ± 2.85
- Median ± IQR
- 5.86 ± 5.46
- Min | Max
- 4.54 | 10.0
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
|
Column
|
Column name
|
dtype
|
Is sorted
|
Null values
|
Unique values
|
Mean
|
Std
|
Min
|
Median
|
Max
|
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Country Name | StringDtype | False | 0 (0.0%) | 3 (100.0%) | |||||
| 1 | Strength of legal rights index (0=weak to 12=strong) | Float64DType | False | 0 (0.0%) | 3 (100.0%) | 6.80 | 2.85 | 4.54 | 5.86 | 10.0 |
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
Country Name
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
Most frequent values
Africa Eastern and Southern
Afghanistan
Africa Western and Central
['Africa Eastern and Southern', 'Afghanistan', 'Africa Western and Central']
Strength of legal rights index (0=weak to 12=strong)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 6.80 ± 2.85
- Median ± IQR
- 5.86 ± 5.46
- Min | Max
- 4.54 | 10.0
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
| Column 1 | Column 2 | Cramér's V | Pearson's Correlation |
|---|---|---|---|
| Country Name | Strength of legal rights index (0=weak to 12=strong) | 1.00 |
Please enable javascript
The skrub table reports need javascript to display correctly. If you are displaying a report in a Jupyter notebook and you see this message, you may need to re-execute the cell or to trust the notebook (button on the top right or "File > Trust notebook").
A correspondence problem#
Alas, the entries for countries do not perfectly match between our original table (df), and those that we downloaded from the worldbank (gdp_per_capita):
df.sort_values(by="Country").tail(7)
| Country | Happiness score | |
|---|---|---|
| 29 | Uruguay | 6.47e+03 |
| 52 | Uzbekistan | 6.06e+03 |
| 107 | Venezuela | 4.92e+03 |
| 76 | Vietnam | 5.48e+03 |
| 131 | Yemen* | 4.20e+03 |
| 136 | Zambia | 3.76e+03 |
| 143 | Zimbabwe | 3.00e+03 |
Country
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 7 (100.0%)
Most frequent values
Uruguay
Uzbekistan
Venezuela
Vietnam
Yemen*
Zambia
Zimbabwe
['Uruguay', 'Uzbekistan', 'Venezuela', 'Vietnam', 'Yemen*', 'Zambia', 'Zimbabwe']
Happiness score
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 7 (100.0%)
- Mean ± Std
- 4.84e+03 ± 1.26e+03
- Median ± IQR
- 4.92e+03 ± 1.29e+03
- Min | Max
- 3.00e+03 | 6.47e+03
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
|
Column
|
Column name
|
dtype
|
Is sorted
|
Null values
|
Unique values
|
Mean
|
Std
|
Min
|
Median
|
Max
|
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Country | StringDtype | True | 0 (0.0%) | 7 (100.0%) | |||||
| 1 | Happiness score | Float64DType | False | 0 (0.0%) | 7 (100.0%) | 4.84e+03 | 1.26e+03 | 3.00e+03 | 4.92e+03 | 6.47e+03 |
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
Country
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 7 (100.0%)
Most frequent values
Uruguay
Uzbekistan
Venezuela
Vietnam
Yemen*
Zambia
Zimbabwe
['Uruguay', 'Uzbekistan', 'Venezuela', 'Vietnam', 'Yemen*', 'Zambia', 'Zimbabwe']
Happiness score
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 7 (100.0%)
- Mean ± Std
- 4.84e+03 ± 1.26e+03
- Median ± IQR
- 4.92e+03 ± 1.29e+03
- Min | Max
- 3.00e+03 | 6.47e+03
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
| Column 1 | Column 2 | Cramér's V | Pearson's Correlation |
|---|---|---|---|
| Country | Happiness score | 1.00 |
Please enable javascript
The skrub table reports need javascript to display correctly. If you are displaying a report in a Jupyter notebook and you see this message, you may need to re-execute the cell or to trust the notebook (button on the top right or "File > Trust notebook").
gdp_per_capita.sort_values(by="Country Name").tail(7)
| Country Name | GDP per capita (current US$) | |
|---|---|---|
| 253 | Vietnam | 4.16e+03 |
| 252 | Virgin Islands (U.S.) | 3.96e+04 |
| 193 | West Bank and Gaza | 3.79e+03 |
| 255 | World | 1.26e+04 |
| 258 | Yemen, Rep. | 702. |
| 260 | Zambia | 1.49e+03 |
| 261 | Zimbabwe | 1.27e+03 |
Country Name
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 7 (100.0%)
Most frequent values
Vietnam
Virgin Islands (U.S.)
West Bank and Gaza
World
Yemen, Rep.
Zambia
Zimbabwe
['Vietnam', 'Virgin Islands (U.S.)', 'West Bank and Gaza', 'World', 'Yemen, Rep.', 'Zambia', 'Zimbabwe']
GDP per capita (current US$)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 7 (100.0%)
- Mean ± Std
- 9.09e+03 ± 1.40e+04
- Median ± IQR
- 3.79e+03 ± 2.68e+03
- Min | Max
- 702. | 3.96e+04
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
|
Column
|
Column name
|
dtype
|
Is sorted
|
Null values
|
Unique values
|
Mean
|
Std
|
Min
|
Median
|
Max
|
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Country Name | StringDtype | True | 0 (0.0%) | 7 (100.0%) | |||||
| 1 | GDP per capita (current US$) | Float64DType | False | 0 (0.0%) | 7 (100.0%) | 9.09e+03 | 1.40e+04 | 702. | 3.79e+03 | 3.96e+04 |
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
Country Name
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 7 (100.0%)
Most frequent values
Vietnam
Virgin Islands (U.S.)
West Bank and Gaza
World
Yemen, Rep.
Zambia
Zimbabwe
['Vietnam', 'Virgin Islands (U.S.)', 'West Bank and Gaza', 'World', 'Yemen, Rep.', 'Zambia', 'Zimbabwe']
GDP per capita (current US$)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 7 (100.0%)
- Mean ± Std
- 9.09e+03 ± 1.40e+04
- Median ± IQR
- 3.79e+03 ± 2.68e+03
- Min | Max
- 702. | 3.96e+04
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
| Column 1 | Column 2 | Cramér's V | Pearson's Correlation |
|---|---|---|---|
| Country Name | GDP per capita (current US$) | 1.00 |
Please enable javascript
The skrub table reports need javascript to display correctly. If you are displaying a report in a Jupyter notebook and you see this message, you may need to re-execute the cell or to trust the notebook (button on the top right or "File > Trust notebook").
We can see that Yemen is written “Yemen*” on one side, and “Yemen, Rep.” on the other.
We also have entries that probably do not have correspondences: “World” on one side, whereas the other table only has country-level data.
Joining tables with imperfect correspondence#
We will now join our initial table, df, with the 3 additional ones that we have extracted.
1. Joining GDP per capita table#
To join them with skrub, we only need to do the following:
from skrub import fuzzy_join
augmented_df = fuzzy_join(
df, # our table to join
gdp_per_capita, # the table to join with
left_on="Country", # the first join key column
right_on="Country Name", # the second join key column
add_match_info=True,
)
augmented_df.tail(20)
# We merged the first World Bank table to our initial one.
| Country | Happiness score | Country Name | GDP per capita (current US$) | skrub_Joiner_distance | skrub_Joiner_rescaled_distance | skrub_Joiner_match_accepted | |
|---|---|---|---|---|---|---|---|
| 126 | Sri Lanka | 4.36e+03 | Sri Lanka | 3.35e+03 | 0.00 | 0.00 | True |
| 127 | Madagascar* | 4.34e+03 | Madagascar | 505. | 0.616 | 0.440 | True |
| 128 | Egypt | 4.29e+03 | Egypt, Arab Rep. | 4.30e+03 | 0.958 | 0.685 | True |
| 129 | Chad* | 4.25e+03 | Chad | 717. | 0.921 | 0.659 | True |
| 130 | Ethiopia | 4.24e+03 | Ethiopia | 1.03e+03 | 0.00 | 0.00 | True |
| 141 | Botswana* | 3.47e+03 | Botswana | 7.74e+03 | 0.639 | 0.457 | True |
| 142 | Rwanda* | 3.27e+03 | Rwanda | 966. | 0.754 | 0.539 | True |
| 143 | Zimbabwe | 3.00e+03 | Zimbabwe | 1.27e+03 | 0.00 | 0.00 | True |
| 144 | Lebanon | 2.96e+03 | Lebanon | 4.14e+03 | 0.00 | 0.00 | True |
| 145 | Afghanistan | 2.40e+03 | Afghanistan | 364. | 0.00 | 0.00 | True |
Country
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 20 (100.0%)
Most frequent values
Sri Lanka
Madagascar*
Egypt
Chad*
Ethiopia
Yemen*
Mauritania*
Jordan
Togo
India
['Sri Lanka', 'Madagascar*', 'Egypt', 'Chad*', 'Ethiopia', 'Yemen*', 'Mauritania*', 'Jordan', 'Togo', 'India']
Happiness score
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 20 (100.0%)
- Mean ± Std
- 3.76e+03 ± 541.
- Median ± IQR
- 3.78e+03 ± 685.
- Min | Max
- 2.40e+03 | 4.36e+03
Country Name
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 20 (100.0%)
Most frequent values
Sri Lanka
Madagascar
Egypt, Arab Rep.
Chad
Ethiopia
Yemen, Rep.
Mauritania
Jordan
Togo
India
['Sri Lanka', 'Madagascar', 'Egypt, Arab Rep.', 'Chad', 'Ethiopia', 'Yemen, Rep.', 'Mauritania', 'Jordan', 'Togo', 'India']
GDP per capita (current US$)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 20 (100.0%)
- Mean ± Std
- 1.98e+03 ± 1.88e+03
- Median ± IQR
- 1.19e+03 ± 1.67e+03
- Min | Max
- 364. | 7.74e+03
skrub_Joiner_distance
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 9 (45.0%)
- Mean ± Std
- 0.311 ± 0.402
- Median ± IQR
- 0.00 ± 0.639
- Min | Max
- 0.00 | 0.989
skrub_Joiner_rescaled_distance
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 9 (45.0%)
- Mean ± Std
- 0.222 ± 0.287
- Median ± IQR
- 0.00 ± 0.457
- Min | Max
- 0.00 | 0.707
skrub_Joiner_match_accepted
BoolDType- Null values
- 0 (0.0%)
True
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
|
Column
|
Column name
|
dtype
|
Is sorted
|
Null values
|
Unique values
|
Mean
|
Std
|
Min
|
Median
|
Max
|
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Country | StringDtype | False | 0 (0.0%) | 20 (100.0%) | |||||
| 1 | Happiness score | Float64DType | True | 0 (0.0%) | 20 (100.0%) | 3.76e+03 | 541. | 2.40e+03 | 3.78e+03 | 4.36e+03 |
| 2 | Country Name | StringDtype | False | 0 (0.0%) | 20 (100.0%) | |||||
| 3 | GDP per capita (current US$) | Float64DType | False | 0 (0.0%) | 20 (100.0%) | 1.98e+03 | 1.88e+03 | 364. | 1.19e+03 | 7.74e+03 |
| 4 | skrub_Joiner_distance | Float64DType | False | 0 (0.0%) | 9 (45.0%) | 0.311 | 0.402 | 0.00 | 0.00 | 0.989 |
| 5 | skrub_Joiner_rescaled_distance | Float64DType | False | 0 (0.0%) | 9 (45.0%) | 0.222 | 0.287 | 0.00 | 0.00 | 0.707 |
| 6 | skrub_Joiner_match_accepted | BoolDType | True | 0 (0.0%) | 1 (5.0%) | 1.00 |
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
Country
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 20 (100.0%)
Most frequent values
Sri Lanka
Madagascar*
Egypt
Chad*
Ethiopia
Yemen*
Mauritania*
Jordan
Togo
India
['Sri Lanka', 'Madagascar*', 'Egypt', 'Chad*', 'Ethiopia', 'Yemen*', 'Mauritania*', 'Jordan', 'Togo', 'India']
Happiness score
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 20 (100.0%)
- Mean ± Std
- 3.76e+03 ± 541.
- Median ± IQR
- 3.78e+03 ± 685.
- Min | Max
- 2.40e+03 | 4.36e+03
Country Name
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 20 (100.0%)
Most frequent values
Sri Lanka
Madagascar
Egypt, Arab Rep.
Chad
Ethiopia
Yemen, Rep.
Mauritania
Jordan
Togo
India
['Sri Lanka', 'Madagascar', 'Egypt, Arab Rep.', 'Chad', 'Ethiopia', 'Yemen, Rep.', 'Mauritania', 'Jordan', 'Togo', 'India']
GDP per capita (current US$)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 20 (100.0%)
- Mean ± Std
- 1.98e+03 ± 1.88e+03
- Median ± IQR
- 1.19e+03 ± 1.67e+03
- Min | Max
- 364. | 7.74e+03
skrub_Joiner_distance
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 9 (45.0%)
- Mean ± Std
- 0.311 ± 0.402
- Median ± IQR
- 0.00 ± 0.639
- Min | Max
- 0.00 | 0.989
skrub_Joiner_rescaled_distance
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 9 (45.0%)
- Mean ± Std
- 0.222 ± 0.287
- Median ± IQR
- 0.00 ± 0.457
- Min | Max
- 0.00 | 0.707
skrub_Joiner_match_accepted
BoolDType- Null values
- 0 (0.0%)
True
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
| Column 1 | Column 2 | Cramér's V | Pearson's Correlation |
|---|---|---|---|
| skrub_Joiner_distance | skrub_Joiner_rescaled_distance | 1.00 | 1.00 |
| Country | Country Name | 1.00 | |
| Happiness score | skrub_Joiner_rescaled_distance | 0.798 | 0.299 |
| Happiness score | skrub_Joiner_distance | 0.798 | 0.299 |
| Country Name | GDP per capita (current US$) | 0.684 | |
| Happiness score | Country Name | 0.684 | |
| Country | GDP per capita (current US$) | 0.684 | |
| Happiness score | GDP per capita (current US$) | 0.684 | 0.0279 |
| Country | Happiness score | 0.684 | |
| GDP per capita (current US$) | skrub_Joiner_distance | 0.635 | 0.0886 |
| GDP per capita (current US$) | skrub_Joiner_rescaled_distance | 0.635 | 0.0886 |
| Country Name | skrub_Joiner_distance | 0.635 | |
| Country | skrub_Joiner_rescaled_distance | 0.635 | |
| Country | skrub_Joiner_distance | 0.635 | |
| Country Name | skrub_Joiner_rescaled_distance | 0.635 | |
| skrub_Joiner_rescaled_distance | skrub_Joiner_match_accepted | 0.00 | |
| skrub_Joiner_distance | skrub_Joiner_match_accepted | 0.00 | |
| GDP per capita (current US$) | skrub_Joiner_match_accepted | 0.00 | |
| Country Name | skrub_Joiner_match_accepted | 0.00 | |
| Happiness score | skrub_Joiner_match_accepted | 0.00 | |
| Country | skrub_Joiner_match_accepted | 0.00 |
Please enable javascript
The skrub table reports need javascript to display correctly. If you are displaying a report in a Jupyter notebook and you see this message, you may need to re-execute the cell or to trust the notebook (button on the top right or "File > Trust notebook").
We see that our fuzzy_join() successfully identified the countries,
even though some country names differ between tables.
For instance, “Egypt” and “Egypt, Arab Rep.” are correctly matched, as are “Lesotho*” and “Lesotho”.
Let’s do some more inspection of the merging done.
Let’s print the worst matches, which will give us an overview of the situation:
augmented_df.sort_values("skrub_Joiner_rescaled_distance").tail(10)
| Country | Happiness score | Country Name | GDP per capita (current US$) | skrub_Joiner_distance | skrub_Joiner_rescaled_distance | skrub_Joiner_match_accepted | |
|---|---|---|---|---|---|---|---|
| 124 | Eswatini, Kingdom of* | 4.40e+03 | Eswatini | 4.04e+03 | 0.983 | 0.703 | True |
| 131 | Yemen* | 4.20e+03 | Yemen, Rep. | 702. | 0.989 | 0.707 | True |
| 34 | Slovakia | 6.39e+03 | Slovak Republic | 2.13e+04 | 0.989 | 0.707 | True |
| 63 | Kyrgyzstan | 5.83e+03 | Kyrgyz Republic | 1.61e+03 | 0.991 | 0.708 | True |
| 109 | Iran | 4.89e+03 | Iraq | 5.94e+03 | 1.06 | 0.755 | True |
| 25 | Taiwan Province of China | 6.51e+03 | China | 1.27e+04 | 1.08 | 0.775 | True |
| 111 | Turkey | 4.74e+03 | Turkiye | 1.06e+04 | 1.13 | 0.811 | True |
| 94 | Laos | 5.14e+03 | Lao PDR | 2.09e+03 | 1.14 | 0.818 | True |
| 121 | Palestinian Territories* | 4.48e+03 | Palau | 1.21e+04 | 1.31 | 0.937 | True |
| 87 | Ivory Coast | 5.24e+03 | East Asia & Pacific | 1.29e+04 | 1.32 | 0.944 | True |
Country
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 10 (100.0%)
Most frequent values
Eswatini, Kingdom of*
Yemen*
Slovakia
Kyrgyzstan
Iran
Taiwan Province of China
Turkey
Laos
Palestinian Territories*
Ivory Coast
['Eswatini, Kingdom of*', 'Yemen*', 'Slovakia', 'Kyrgyzstan', 'Iran', 'Taiwan Province of China', 'Turkey', 'Laos', 'Palestinian Territories*', 'Ivory Coast']
Happiness score
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 10 (100.0%)
- Mean ± Std
- 5.18e+03 ± 817.
- Median ± IQR
- 4.89e+03 ± 1.34e+03
- Min | Max
- 4.20e+03 | 6.51e+03
Country Name
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 10 (100.0%)
Most frequent values
Eswatini
Yemen, Rep.
Slovak Republic
Kyrgyz Republic
Iraq
China
Turkiye
Lao PDR
Palau
East Asia & Pacific
['Eswatini', 'Yemen, Rep.', 'Slovak Republic', 'Kyrgyz Republic', 'Iraq', 'China', 'Turkiye', 'Lao PDR', 'Palau', 'East Asia & Pacific']
GDP per capita (current US$)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 10 (100.0%)
- Mean ± Std
- 8.40e+03 ± 6.61e+03
- Median ± IQR
- 5.94e+03 ± 1.06e+04
- Min | Max
- 702. | 2.13e+04
skrub_Joiner_distance
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 10 (100.0%)
- Mean ± Std
- 1.10 ± 0.128
- Median ± IQR
- 1.06 ± 0.156
- Min | Max
- 0.983 | 1.32
skrub_Joiner_rescaled_distance
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 10 (100.0%)
- Mean ± Std
- 0.787 ± 0.0918
- Median ± IQR
- 0.755 ± 0.111
- Min | Max
- 0.703 | 0.944
skrub_Joiner_match_accepted
BoolDType- Null values
- 0 (0.0%)
True
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
|
Column
|
Column name
|
dtype
|
Is sorted
|
Null values
|
Unique values
|
Mean
|
Std
|
Min
|
Median
|
Max
|
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Country | StringDtype | False | 0 (0.0%) | 10 (100.0%) | |||||
| 1 | Happiness score | Float64DType | False | 0 (0.0%) | 10 (100.0%) | 5.18e+03 | 817. | 4.20e+03 | 4.89e+03 | 6.51e+03 |
| 2 | Country Name | StringDtype | False | 0 (0.0%) | 10 (100.0%) | |||||
| 3 | GDP per capita (current US$) | Float64DType | False | 0 (0.0%) | 10 (100.0%) | 8.40e+03 | 6.61e+03 | 702. | 5.94e+03 | 2.13e+04 |
| 4 | skrub_Joiner_distance | Float64DType | True | 0 (0.0%) | 10 (100.0%) | 1.10 | 0.128 | 0.983 | 1.06 | 1.32 |
| 5 | skrub_Joiner_rescaled_distance | Float64DType | True | 0 (0.0%) | 10 (100.0%) | 0.787 | 0.0918 | 0.703 | 0.755 | 0.944 |
| 6 | skrub_Joiner_match_accepted | BoolDType | True | 0 (0.0%) | 1 (10.0%) | 1.00 |
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
Country
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 10 (100.0%)
Most frequent values
Eswatini, Kingdom of*
Yemen*
Slovakia
Kyrgyzstan
Iran
Taiwan Province of China
Turkey
Laos
Palestinian Territories*
Ivory Coast
['Eswatini, Kingdom of*', 'Yemen*', 'Slovakia', 'Kyrgyzstan', 'Iran', 'Taiwan Province of China', 'Turkey', 'Laos', 'Palestinian Territories*', 'Ivory Coast']
Happiness score
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 10 (100.0%)
- Mean ± Std
- 5.18e+03 ± 817.
- Median ± IQR
- 4.89e+03 ± 1.34e+03
- Min | Max
- 4.20e+03 | 6.51e+03
Country Name
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 10 (100.0%)
Most frequent values
Eswatini
Yemen, Rep.
Slovak Republic
Kyrgyz Republic
Iraq
China
Turkiye
Lao PDR
Palau
East Asia & Pacific
['Eswatini', 'Yemen, Rep.', 'Slovak Republic', 'Kyrgyz Republic', 'Iraq', 'China', 'Turkiye', 'Lao PDR', 'Palau', 'East Asia & Pacific']
GDP per capita (current US$)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 10 (100.0%)
- Mean ± Std
- 8.40e+03 ± 6.61e+03
- Median ± IQR
- 5.94e+03 ± 1.06e+04
- Min | Max
- 702. | 2.13e+04
skrub_Joiner_distance
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 10 (100.0%)
- Mean ± Std
- 1.10 ± 0.128
- Median ± IQR
- 1.06 ± 0.156
- Min | Max
- 0.983 | 1.32
skrub_Joiner_rescaled_distance
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 10 (100.0%)
- Mean ± Std
- 0.787 ± 0.0918
- Median ± IQR
- 0.755 ± 0.111
- Min | Max
- 0.703 | 0.944
skrub_Joiner_match_accepted
BoolDType- Null values
- 0 (0.0%)
True
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
| Column 1 | Column 2 | Cramér's V | Pearson's Correlation |
|---|---|---|---|
| GDP per capita (current US$) | skrub_Joiner_rescaled_distance | 1.00 | 0.299 |
| Country Name | GDP per capita (current US$) | 1.00 | |
| Country Name | skrub_Joiner_distance | 1.00 | |
| Country Name | skrub_Joiner_rescaled_distance | 1.00 | |
| GDP per capita (current US$) | skrub_Joiner_distance | 1.00 | 0.299 |
| Country | Country Name | 1.00 | |
| Country | Happiness score | 1.00 | |
| Country | GDP per capita (current US$) | 1.00 | |
| Country | skrub_Joiner_distance | 1.00 | |
| Happiness score | skrub_Joiner_distance | 1.00 | -0.173 |
| Happiness score | skrub_Joiner_rescaled_distance | 1.00 | -0.173 |
| Country | skrub_Joiner_rescaled_distance | 1.00 | |
| Happiness score | GDP per capita (current US$) | 1.00 | 0.527 |
| Happiness score | Country Name | 1.00 | |
| skrub_Joiner_distance | skrub_Joiner_rescaled_distance | 1.00 | 1.00 |
| skrub_Joiner_rescaled_distance | skrub_Joiner_match_accepted | 0.00 | |
| skrub_Joiner_distance | skrub_Joiner_match_accepted | 0.00 | |
| GDP per capita (current US$) | skrub_Joiner_match_accepted | 0.00 | |
| Country Name | skrub_Joiner_match_accepted | 0.00 | |
| Happiness score | skrub_Joiner_match_accepted | 0.00 | |
| Country | skrub_Joiner_match_accepted | 0.00 |
Please enable javascript
The skrub table reports need javascript to display correctly. If you are displaying a report in a Jupyter notebook and you see this message, you may need to re-execute the cell or to trust the notebook (button on the top right or "File > Trust notebook").
We see that some matches were unsuccessful (e.g “Palestinian Territories*” and “Palau”), because there is simply no match in the two tables.
In this case, it is better to use the threshold parameter (max_dist)
so as to include only precise-enough matches:
augmented_df = fuzzy_join(
df,
gdp_per_capita,
left_on="Country",
right_on="Country Name",
max_dist=0.9,
add_match_info=True,
)
augmented_df.sort_values("skrub_Joiner_rescaled_distance", ascending=False).head()
| Country | Happiness score | Country Name | GDP per capita (current US$) | skrub_Joiner_distance | skrub_Joiner_rescaled_distance | skrub_Joiner_match_accepted | |
|---|---|---|---|---|---|---|---|
| 87 | Ivory Coast | 5.24e+03 | 1.32 | 0.944 | False | ||
| 121 | Palestinian Territories* | 4.48e+03 | 1.31 | 0.937 | False | ||
| 94 | Laos | 5.14e+03 | Lao PDR | 2.09e+03 | 1.14 | 0.818 | True |
| 111 | Turkey | 4.74e+03 | Turkiye | 1.06e+04 | 1.13 | 0.811 | True |
| 25 | Taiwan Province of China | 6.51e+03 | China | 1.27e+04 | 1.08 | 0.775 | True |
Country
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 5 (100.0%)
Most frequent values
Ivory Coast
Palestinian Territories*
Laos
Turkey
Taiwan Province of China
['Ivory Coast', 'Palestinian Territories*', 'Laos', 'Turkey', 'Taiwan Province of China']
Happiness score
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 5 (100.0%)
- Mean ± Std
- 5.22e+03 ± 782.
- Median ± IQR
- 5.14e+03 ± 491.
- Min | Max
- 4.48e+03 | 6.51e+03
Country Name
StringDtype- Null values
- 2 (40.0%)
- Unique values
- 3 (60.0%)
Most frequent values
Lao PDR
Turkiye
China
['Lao PDR', 'Turkiye', 'China']
GDP per capita (current US$)
Float64DType- Null values
- 2 (40.0%)
- Unique values
- 3 (60.0%)
- Mean ± Std
- 8.47e+03 ± 5.63e+03
- Median ± IQR
- 1.06e+04 ± 1.06e+04
- Min | Max
- 2.09e+03 | 1.27e+04
skrub_Joiner_distance
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 5 (100.0%)
- Mean ± Std
- 1.20 ± 0.109
- Median ± IQR
- 1.14 ± 0.176
- Min | Max
- 1.08 | 1.32
skrub_Joiner_rescaled_distance
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 5 (100.0%)
- Mean ± Std
- 0.857 ± 0.0777
- Median ± IQR
- 0.818 ± 0.126
- Min | Max
- 0.775 | 0.944
skrub_Joiner_match_accepted
BoolDType- Null values
- 0 (0.0%)
- Unique values
- 2 (40.0%)
- Mean
- 0.600
Most frequent values
True
False
[True, False]
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
|
Column
|
Column name
|
dtype
|
Is sorted
|
Null values
|
Unique values
|
Mean
|
Std
|
Min
|
Median
|
Max
|
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Country | StringDtype | False | 0 (0.0%) | 5 (100.0%) | |||||
| 1 | Happiness score | Float64DType | False | 0 (0.0%) | 5 (100.0%) | 5.22e+03 | 782. | 4.48e+03 | 5.14e+03 | 6.51e+03 |
| 2 | Country Name | StringDtype | False | 2 (40.0%) | 3 (60.0%) | |||||
| 3 | GDP per capita (current US$) | Float64DType | True | 2 (40.0%) | 3 (60.0%) | 8.47e+03 | 5.63e+03 | 2.09e+03 | 1.06e+04 | 1.27e+04 |
| 4 | skrub_Joiner_distance | Float64DType | True | 0 (0.0%) | 5 (100.0%) | 1.20 | 0.109 | 1.08 | 1.14 | 1.32 |
| 5 | skrub_Joiner_rescaled_distance | Float64DType | True | 0 (0.0%) | 5 (100.0%) | 0.857 | 0.0777 | 0.775 | 0.818 | 0.944 |
| 6 | skrub_Joiner_match_accepted | BoolDType | True | 0 (0.0%) | 2 (40.0%) | 0.600 |
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
Country
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 5 (100.0%)
Most frequent values
Ivory Coast
Palestinian Territories*
Laos
Turkey
Taiwan Province of China
['Ivory Coast', 'Palestinian Territories*', 'Laos', 'Turkey', 'Taiwan Province of China']
Happiness score
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 5 (100.0%)
- Mean ± Std
- 5.22e+03 ± 782.
- Median ± IQR
- 5.14e+03 ± 491.
- Min | Max
- 4.48e+03 | 6.51e+03
Country Name
StringDtype- Null values
- 2 (40.0%)
- Unique values
- 3 (60.0%)
Most frequent values
Lao PDR
Turkiye
China
['Lao PDR', 'Turkiye', 'China']
GDP per capita (current US$)
Float64DType- Null values
- 2 (40.0%)
- Unique values
- 3 (60.0%)
- Mean ± Std
- 8.47e+03 ± 5.63e+03
- Median ± IQR
- 1.06e+04 ± 1.06e+04
- Min | Max
- 2.09e+03 | 1.27e+04
skrub_Joiner_distance
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 5 (100.0%)
- Mean ± Std
- 1.20 ± 0.109
- Median ± IQR
- 1.14 ± 0.176
- Min | Max
- 1.08 | 1.32
skrub_Joiner_rescaled_distance
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 5 (100.0%)
- Mean ± Std
- 0.857 ± 0.0777
- Median ± IQR
- 0.818 ± 0.126
- Min | Max
- 0.775 | 0.944
skrub_Joiner_match_accepted
BoolDType- Null values
- 0 (0.0%)
- Unique values
- 2 (40.0%)
- Mean
- 0.600
Most frequent values
True
False
[True, False]
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
| Column 1 | Column 2 | Cramér's V | Pearson's Correlation |
|---|---|---|---|
| skrub_Joiner_rescaled_distance | skrub_Joiner_match_accepted | 1.00 | -0.977 |
| skrub_Joiner_distance | skrub_Joiner_match_accepted | 1.00 | -0.977 |
| skrub_Joiner_distance | skrub_Joiner_rescaled_distance | 1.00 | 1.00 |
| GDP per capita (current US$) | skrub_Joiner_match_accepted | 1.00 | |
| GDP per capita (current US$) | skrub_Joiner_rescaled_distance | 1.00 | -0.764 |
| GDP per capita (current US$) | skrub_Joiner_distance | 1.00 | -0.764 |
| Country Name | skrub_Joiner_match_accepted | 1.00 | |
| Country Name | skrub_Joiner_rescaled_distance | 1.00 | |
| Country Name | skrub_Joiner_distance | 1.00 | |
| Country Name | GDP per capita (current US$) | 1.00 | |
| Happiness score | skrub_Joiner_match_accepted | 1.00 | 0.425 |
| Happiness score | skrub_Joiner_rescaled_distance | 1.00 | -0.569 |
| Happiness score | skrub_Joiner_distance | 1.00 | -0.569 |
| Happiness score | GDP per capita (current US$) | 1.00 | 0.476 |
| Happiness score | Country Name | 1.00 | |
| Country | skrub_Joiner_match_accepted | 1.00 | |
| Country | skrub_Joiner_rescaled_distance | 1.00 | |
| Country | skrub_Joiner_distance | 1.00 | |
| Country | GDP per capita (current US$) | 1.00 | |
| Country | Country Name | 1.00 | |
| Country | Happiness score | 1.00 |
Please enable javascript
The skrub table reports need javascript to display correctly. If you are displaying a report in a Jupyter notebook and you see this message, you may need to re-execute the cell or to trust the notebook (button on the top right or "File > Trust notebook").
Matches that are not available (or precise enough) are marked as NaN.
We will remove them using the drop_unmatched parameter:
augmented_df = fuzzy_join(
df,
gdp_per_capita,
left_on="Country",
right_on="Country Name",
drop_unmatched=True,
max_dist=0.9,
add_match_info=True,
)
augmented_df.drop(columns=["Country Name"], inplace=True)
We can finally plot and look at the link between GDP per capital and happiness:
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_context("notebook")
plt.figure(figsize=(4, 3))
ax = sns.regplot(
data=augmented_df,
x="GDP per capita (current US$)",
y="Happiness score",
lowess=True,
)
ax.set_ylabel("Happiness index")
ax.set_title("Is a higher GDP per capita linked to happiness?")
plt.tight_layout()
plt.show()

It seems that the happiest countries are those having a high GDP per capita. However, unhappy countries do not have only low levels of GDP per capita. We have to search for other patterns.
2. Joining life expectancy table#
Now let’s include other information that may be relevant, such as in the life_exp table:
augmented_df = fuzzy_join(
augmented_df,
life_exp,
left_on="Country",
right_on="Country Name",
max_dist=0.9,
add_match_info=True,
)
augmented_df.drop(columns=["Country Name"], inplace=True)
augmented_df.head(3)
| Country | Happiness score | GDP per capita (current US$) | skrub_Joiner_distance | skrub_Joiner_rescaled_distance | skrub_Joiner_match_accepted | Life expectancy at birth, total (years) | |
|---|---|---|---|---|---|---|---|
| 0 | Finland | 7.82e+03 | 5.05e+04 | 0.00 | 0.00 | True | 81.9 |
| 1 | Denmark | 7.64e+03 | 6.70e+04 | 0.00 | 0.00 | True | 81.4 |
| 2 | Iceland | 7.56e+03 | 7.29e+04 | 0.00 | 0.00 | True | 83.1 |
Country
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
Most frequent values
Finland
Denmark
Iceland
['Finland', 'Denmark', 'Iceland']
Happiness score
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 7.67e+03 ± 136.
- Median ± IQR
- 7.64e+03 ± 264.
- Min | Max
- 7.56e+03 | 7.82e+03
GDP per capita (current US$)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 6.35e+04 ± 1.16e+04
- Median ± IQR
- 6.70e+04 ± 2.24e+04
- Min | Max
- 5.05e+04 | 7.29e+04
skrub_Joiner_distance
Float64DType- Null values
- 0 (0.0%)
0.0
skrub_Joiner_rescaled_distance
Float64DType- Null values
- 0 (0.0%)
0.0
skrub_Joiner_match_accepted
BoolDType- Null values
- 0 (0.0%)
True
Life expectancy at birth, total (years)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 82.2 ± 0.877
- Median ± IQR
- 81.9 ± 1.71
- Min | Max
- 81.4 | 83.1
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
|
Column
|
Column name
|
dtype
|
Is sorted
|
Null values
|
Unique values
|
Mean
|
Std
|
Min
|
Median
|
Max
|
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Country | StringDtype | False | 0 (0.0%) | 3 (100.0%) | |||||
| 1 | Happiness score | Float64DType | True | 0 (0.0%) | 3 (100.0%) | 7.67e+03 | 136. | 7.56e+03 | 7.64e+03 | 7.82e+03 |
| 2 | GDP per capita (current US$) | Float64DType | True | 0 (0.0%) | 3 (100.0%) | 6.35e+04 | 1.16e+04 | 5.05e+04 | 6.70e+04 | 7.29e+04 |
| 3 | skrub_Joiner_distance | Float64DType | True | 0 (0.0%) | 1 (33.3%) | 0.00 | 0.00 | |||
| 4 | skrub_Joiner_rescaled_distance | Float64DType | True | 0 (0.0%) | 1 (33.3%) | 0.00 | 0.00 | |||
| 5 | skrub_Joiner_match_accepted | BoolDType | True | 0 (0.0%) | 1 (33.3%) | 1.00 | ||||
| 6 | Life expectancy at birth, total (years) | Float64DType | False | 0 (0.0%) | 3 (100.0%) | 82.2 | 0.877 | 81.4 | 81.9 | 83.1 |
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
Country
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
Most frequent values
Finland
Denmark
Iceland
['Finland', 'Denmark', 'Iceland']
Happiness score
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 7.67e+03 ± 136.
- Median ± IQR
- 7.64e+03 ± 264.
- Min | Max
- 7.56e+03 | 7.82e+03
GDP per capita (current US$)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 6.35e+04 ± 1.16e+04
- Median ± IQR
- 6.70e+04 ± 2.24e+04
- Min | Max
- 5.05e+04 | 7.29e+04
skrub_Joiner_distance
Float64DType- Null values
- 0 (0.0%)
0.0
skrub_Joiner_rescaled_distance
Float64DType- Null values
- 0 (0.0%)
0.0
skrub_Joiner_match_accepted
BoolDType- Null values
- 0 (0.0%)
True
Life expectancy at birth, total (years)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 82.2 ± 0.877
- Median ± IQR
- 81.9 ± 1.71
- Min | Max
- 81.4 | 83.1
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
| Column 1 | Column 2 | Cramér's V | Pearson's Correlation |
|---|---|---|---|
| Country | Happiness score | 1.00 | |
| Happiness score | GDP per capita (current US$) | 1.00 | -0.999 |
| Country | Life expectancy at birth, total (years) | 1.00 | |
| Happiness score | Life expectancy at birth, total (years) | 1.00 | -0.491 |
| GDP per capita (current US$) | Life expectancy at birth, total (years) | 1.00 | 0.458 |
| Country | GDP per capita (current US$) | 1.00 | |
| skrub_Joiner_distance | skrub_Joiner_match_accepted | 0.00 | |
| skrub_Joiner_rescaled_distance | skrub_Joiner_match_accepted | 0.00 | |
| skrub_Joiner_rescaled_distance | Life expectancy at birth, total (years) | 0.00 | |
| skrub_Joiner_match_accepted | Life expectancy at birth, total (years) | 0.00 | |
| skrub_Joiner_distance | Life expectancy at birth, total (years) | 0.00 | |
| GDP per capita (current US$) | skrub_Joiner_match_accepted | 0.00 | |
| skrub_Joiner_distance | skrub_Joiner_rescaled_distance | 0.00 | |
| Happiness score | skrub_Joiner_rescaled_distance | 0.00 | |
| Happiness score | skrub_Joiner_match_accepted | 0.00 | |
| GDP per capita (current US$) | skrub_Joiner_distance | 0.00 | |
| GDP per capita (current US$) | skrub_Joiner_rescaled_distance | 0.00 | |
| Country | skrub_Joiner_match_accepted | 0.00 | |
| Happiness score | skrub_Joiner_distance | 0.00 | |
| Country | skrub_Joiner_distance | 0.00 | |
| Country | skrub_Joiner_rescaled_distance | 0.00 |
Please enable javascript
The skrub table reports need javascript to display correctly. If you are displaying a report in a Jupyter notebook and you see this message, you may need to re-execute the cell or to trust the notebook (button on the top right or "File > Trust notebook").
Let’s plot this relation:
plt.figure(figsize=(4, 3))
fig = sns.regplot(
data=augmented_df,
x="Life expectancy at birth, total (years)",
y="Happiness score",
lowess=True,
)
fig.set_ylabel("Happiness index")
fig.set_title("Is a higher life expectancy linked to happiness?")
plt.tight_layout()
plt.show()

It seems the answer is yes! Countries with higher life expectancy are also happier.
3. Joining legal rights strength table#
And the table with a measure of legal rights strength in the country:
augmented_df = fuzzy_join(
augmented_df,
legal_rights,
left_on="Country",
right_on="Country Name",
max_dist=0.9,
add_match_info=True,
)
augmented_df.drop(columns=["Country Name"], inplace=True)
augmented_df.head(3)
| Country | Happiness score | GDP per capita (current US$) | skrub_Joiner_distance | skrub_Joiner_rescaled_distance | skrub_Joiner_match_accepted | Life expectancy at birth, total (years) | Strength of legal rights index (0=weak to 12=strong) | |
|---|---|---|---|---|---|---|---|---|
| 0 | Finland | 7.82e+03 | 5.05e+04 | 0.00 | 0.00 | True | 81.9 | 6.00 |
| 1 | Denmark | 7.64e+03 | 6.70e+04 | 0.00 | 0.00 | True | 81.4 | 8.00 |
| 2 | Iceland | 7.56e+03 | 7.29e+04 | 0.00 | 0.00 | True | 83.1 | 4.00 |
Country
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
Most frequent values
Finland
Denmark
Iceland
['Finland', 'Denmark', 'Iceland']
Happiness score
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 7.67e+03 ± 136.
- Median ± IQR
- 7.64e+03 ± 264.
- Min | Max
- 7.56e+03 | 7.82e+03
GDP per capita (current US$)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 6.35e+04 ± 1.16e+04
- Median ± IQR
- 6.70e+04 ± 2.24e+04
- Min | Max
- 5.05e+04 | 7.29e+04
skrub_Joiner_distance
Float64DType- Null values
- 0 (0.0%)
0.0
skrub_Joiner_rescaled_distance
Float64DType- Null values
- 0 (0.0%)
0.0
skrub_Joiner_match_accepted
BoolDType- Null values
- 0 (0.0%)
True
Life expectancy at birth, total (years)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 82.2 ± 0.877
- Median ± IQR
- 81.9 ± 1.71
- Min | Max
- 81.4 | 83.1
Strength of legal rights index (0=weak to 12=strong)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 6.00 ± 2.00
- Median ± IQR
- 6.00 ± 4.00
- Min | Max
- 4.00 | 8.00
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
|
Column
|
Column name
|
dtype
|
Is sorted
|
Null values
|
Unique values
|
Mean
|
Std
|
Min
|
Median
|
Max
|
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Country | StringDtype | False | 0 (0.0%) | 3 (100.0%) | |||||
| 1 | Happiness score | Float64DType | True | 0 (0.0%) | 3 (100.0%) | 7.67e+03 | 136. | 7.56e+03 | 7.64e+03 | 7.82e+03 |
| 2 | GDP per capita (current US$) | Float64DType | True | 0 (0.0%) | 3 (100.0%) | 6.35e+04 | 1.16e+04 | 5.05e+04 | 6.70e+04 | 7.29e+04 |
| 3 | skrub_Joiner_distance | Float64DType | True | 0 (0.0%) | 1 (33.3%) | 0.00 | 0.00 | |||
| 4 | skrub_Joiner_rescaled_distance | Float64DType | True | 0 (0.0%) | 1 (33.3%) | 0.00 | 0.00 | |||
| 5 | skrub_Joiner_match_accepted | BoolDType | True | 0 (0.0%) | 1 (33.3%) | 1.00 | ||||
| 6 | Life expectancy at birth, total (years) | Float64DType | False | 0 (0.0%) | 3 (100.0%) | 82.2 | 0.877 | 81.4 | 81.9 | 83.1 |
| 7 | Strength of legal rights index (0=weak to 12=strong) | Float64DType | False | 0 (0.0%) | 3 (100.0%) | 6.00 | 2.00 | 4.00 | 6.00 | 8.00 |
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
Country
StringDtype- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
Most frequent values
Finland
Denmark
Iceland
['Finland', 'Denmark', 'Iceland']
Happiness score
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 7.67e+03 ± 136.
- Median ± IQR
- 7.64e+03 ± 264.
- Min | Max
- 7.56e+03 | 7.82e+03
GDP per capita (current US$)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 6.35e+04 ± 1.16e+04
- Median ± IQR
- 6.70e+04 ± 2.24e+04
- Min | Max
- 5.05e+04 | 7.29e+04
skrub_Joiner_distance
Float64DType- Null values
- 0 (0.0%)
0.0
skrub_Joiner_rescaled_distance
Float64DType- Null values
- 0 (0.0%)
0.0
skrub_Joiner_match_accepted
BoolDType- Null values
- 0 (0.0%)
True
Life expectancy at birth, total (years)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 82.2 ± 0.877
- Median ± IQR
- 81.9 ± 1.71
- Min | Max
- 81.4 | 83.1
Strength of legal rights index (0=weak to 12=strong)
Float64DType- Null values
- 0 (0.0%)
- Unique values
- 3 (100.0%)
- Mean ± Std
- 6.00 ± 2.00
- Median ± IQR
- 6.00 ± 4.00
- Min | Max
- 4.00 | 8.00
No columns match the selected filter: . You can change the column filter in the dropdown menu above.
| Column 1 | Column 2 | Cramér's V | Pearson's Correlation |
|---|---|---|---|
| Life expectancy at birth, total (years) | Strength of legal rights index (0=weak to 12=strong) | 1.00 | -0.977 |
| GDP per capita (current US$) | Strength of legal rights index (0=weak to 12=strong) | 1.00 | -0.255 |
| GDP per capita (current US$) | Life expectancy at birth, total (years) | 1.00 | 0.458 |
| Happiness score | Strength of legal rights index (0=weak to 12=strong) | 1.00 | 0.292 |
| Country | GDP per capita (current US$) | 1.00 | |
| Happiness score | GDP per capita (current US$) | 1.00 | -0.999 |
| Country | Life expectancy at birth, total (years) | 1.00 | |
| Country | Happiness score | 1.00 | |
| Happiness score | Life expectancy at birth, total (years) | 1.00 | -0.491 |
| Country | Strength of legal rights index (0=weak to 12=strong) | 1.00 | |
| skrub_Joiner_rescaled_distance | Strength of legal rights index (0=weak to 12=strong) | 0.00 | |
| skrub_Joiner_match_accepted | Strength of legal rights index (0=weak to 12=strong) | 0.00 | |
| skrub_Joiner_distance | skrub_Joiner_match_accepted | 0.00 | |
| skrub_Joiner_distance | skrub_Joiner_rescaled_distance | 0.00 | |
| skrub_Joiner_distance | Strength of legal rights index (0=weak to 12=strong) | 0.00 | |
| skrub_Joiner_distance | Life expectancy at birth, total (years) | 0.00 | |
| skrub_Joiner_rescaled_distance | skrub_Joiner_match_accepted | 0.00 | |
| skrub_Joiner_rescaled_distance | Life expectancy at birth, total (years) | 0.00 | |
| skrub_Joiner_match_accepted | Life expectancy at birth, total (years) | 0.00 | |
| GDP per capita (current US$) | skrub_Joiner_rescaled_distance | 0.00 | |
| Happiness score | skrub_Joiner_distance | 0.00 | |
| Happiness score | skrub_Joiner_rescaled_distance | 0.00 | |
| Happiness score | skrub_Joiner_match_accepted | 0.00 | |
| GDP per capita (current US$) | skrub_Joiner_distance | 0.00 | |
| GDP per capita (current US$) | skrub_Joiner_match_accepted | 0.00 | |
| Country | skrub_Joiner_match_accepted | 0.00 | |
| Country | skrub_Joiner_distance | 0.00 | |
| Country | skrub_Joiner_rescaled_distance | 0.00 |
Please enable javascript
The skrub table reports need javascript to display correctly. If you are displaying a report in a Jupyter notebook and you see this message, you may need to re-execute the cell or to trust the notebook (button on the top right or "File > Trust notebook").
Let’s take a look at their correspondence in a figure:
plt.figure(figsize=(4, 3))
fig = sns.regplot(
data=augmented_df,
x="Strength of legal rights index (0=weak to 12=strong)",
y="Happiness score",
lowess=True,
)
fig.set_ylabel("Happiness index")
fig.set_title("Does a country's legal rights strength lead to happiness?")
plt.tight_layout()
plt.show()

From this plot, it is not clear that this measure of legal strength is linked to happiness.
Great! Our joined table has become bigger and full of useful information. And now we are ready to apply a first machine learning model to it!
Prediction model#
We now separate our covariates (X), from the target (or exogenous) variables: y.
y = augmented_df["Happiness score"]
X = augmented_df.drop(["Happiness score", "Country"], axis=1)
Let us now define the model that will be used to predict the happiness score:
from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.model_selection import KFold
hgdb = HistGradientBoostingRegressor(random_state=0)
cv = KFold(n_splits=5, shuffle=True, random_state=0)
To evaluate our model, we will apply a 5-fold cross-validation.
We evaluate our model using the R2 score.
Let’s finally assess the results of our models:
from sklearn.model_selection import cross_validate
cv_results_t = cross_validate(hgdb, X, y, cv=cv, scoring="r2")
cv_r2_t = cv_results_t["test_score"]
print(f"Mean R² score is {cv_r2_t.mean():.2f} +- {cv_r2_t.std():.2f}")
Mean R² score is 0.65 +- 0.08
We have a satisfying first result: an R² of 0.63!
Data cleaning varies from dataset to dataset: there are as
many ways to clean a table as there are errors. The fuzzy_join()
method is generalizable across all datasets.
Data transformation is also often very costly in both time and resources.
fuzzy_join() is fast and easy-to-use.
Now up to you, try improving our model by adding information into it and beating our result!
Using the Joiner() to fuzzy join multiple tables#
A convenient way to merge different tables from the World Bank
to X in a scikit-learn Pipeline and tune the parameters is to use the Joiner().
The Joiner() is a transformer that can fuzzy-join a table on
a main table.
Instantiating the transformer#
y = df["Happiness score"]
df = df.drop("Happiness score", axis=1)
from sklearn.pipeline import make_pipeline
from skrub import Joiner, SelectCols
# We create a selector that we will insert at the end of our pipeline, to
# select the relevant columns before fitting the regressor
selector = SelectCols(
[
"GDP per capita (current US$) gdp",
"Life expectancy at birth, total (years) life_exp",
"Strength of legal rights index (0=weak to 12=strong) legal_rights",
]
)
# And we can now put together the pipeline
pipeline = make_pipeline(
Joiner(gdp_per_capita, main_key="Country", aux_key="Country Name", suffix=" gdp"),
Joiner(life_exp, main_key="Country", aux_key="Country Name", suffix=" life_exp"),
Joiner(
legal_rights, main_key="Country", aux_key="Country Name", suffix=" legal_rights"
),
selector,
HistGradientBoostingRegressor(),
)
And the best part is that we are now able to evaluate the parameters of the fuzzy_join().
For instance, the match_score was manually picked and can now be
introduced into a grid search:
from sklearn.model_selection import GridSearchCV
# We will test 2 possible values of max_dist:
params = {
"joiner-1__max_dist": [0.1, 0.9],
"joiner-2__max_dist": [0.1, 0.9],
"joiner-3__max_dist": [0.1, 0.9],
}
grid = GridSearchCV(pipeline, param_grid=params, cv=cv)
grid.fit(df, y)
print("Best parameters:", grid.best_params_)
Best parameters: {'joiner-1__max_dist': 0.1, 'joiner-2__max_dist': 0.9, 'joiner-3__max_dist': 0.1}
Total running time of the script: (0 minutes 35.584 seconds)
Estimated memory usage: 622 MB