<jupyter_start><jupyter_text>Joining data in pandasIn this notebook, we'll use pandas to join some relational data:- `../data/country-codes.csv` -- a table of ISO country codes and country names- `../data/country-population.csv` -- country population data from the U.N.👉 [Read more about the `merge` method for joining dataframes](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html)<jupyter_code>import pandas as pd<jupyter_output><empty_output><jupyter_text>When we read in the CSVs, we need to make sure that pandas doesn't parse the ISO codes as numbers, because we want to keep any leading zeroes. So in addition to the path to the CSV, we'll also use an argument called `dtype` to specify that the `code` columns need to be parsed as a string.👉 You can find more information on the `dtype` argument [in the documentation for the `read_csv()` method](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html)<jupyter_code>country_codes = pd.read_csv('../data/country-codes.csv', dtype={'code': str})
country_codes.head()
country_pop = pd.read_csv('../data/country-population.csv', dtype={'code': str})
country_pop.head()<jupyter_output><empty_output><jupyter_text>Join the data with the country codes lookup tableTo join data in pandas, we can use the [`merge()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html) method. At minimum, you need to hand this method the two dataframes to join, plus specify the name of the column to join `on`. (If the columns have different names, you can use the `left_on` and `right_on` arguments -- the "left" dataframe is the first one you hand to the `merge` method.)<jupyter_code>merged = pd.merge(country_pop,
                  country_codes,
                  on='code')
merged.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11189189189189189, 'fraction_numerical': 0.0, 'mean_word_length': 4.784375, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '99151', 'license': 'MIT', 'n_tokens_mistral': 571, 'n_tokens_neox': 557, 'n_words': 196, 'path': 'python/notebooks/Pandas - Joining data.ipynb', 'repo_name': 'ireapps/jni-intermediate-2021', 'size': '1850'}