Combining Data Tables in Pandas

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will work with air quality data to explore how to combine multiple tables using Python's Pandas library. We will be using the concat and merge functions to perform these operations. This lab will help you understand how to concatenate and merge data frames effectively.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) pandas(("`Pandas`")) -.-> pandas/ReadingDataGroup(["`Reading Data`"]) pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) pandas(("`Pandas`")) -.-> pandas/AdvancedOperationsGroup(["`Advanced Operations`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") pandas/ReadingDataGroup -.-> pandas/read_csv("`Read CSV`") pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") pandas/AdvancedOperationsGroup -.-> pandas/merge_data("`Merging Data`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") subgraph Lab Skills python/comments -.-> lab-65437{{"`Combining Data Tables in Pandas`"}} pandas/read_csv -.-> lab-65437{{"`Combining Data Tables in Pandas`"}} pandas/select_columns -.-> lab-65437{{"`Combining Data Tables in Pandas`"}} pandas/merge_data -.-> lab-65437{{"`Combining Data Tables in Pandas`"}} python/booleans -.-> lab-65437{{"`Combining Data Tables in Pandas`"}} python/lists -.-> lab-65437{{"`Combining Data Tables in Pandas`"}} python/tuples -.-> lab-65437{{"`Combining Data Tables in Pandas`"}} python/importing_modules -.-> lab-65437{{"`Combining Data Tables in Pandas`"}} python/standard_libraries -.-> lab-65437{{"`Combining Data Tables in Pandas`"}} python/numerical_computing -.-> lab-65437{{"`Combining Data Tables in Pandas`"}} python/data_analysis -.-> lab-65437{{"`Combining Data Tables in Pandas`"}} end

Import Required Libraries

Our first step is to import the libraries we will need. For this lab, we will be using the pandas library.

## Import the required library
import pandas as pd

Load the Datasets

We will load two datasets related to air quality. One contains Nitrate data and the other contains Particulate matter data.

## Load the Nitrate data
air_quality_no2 = pd.read_csv("data/air_quality_no2_long.csv", parse_dates=True)
air_quality_no2 = air_quality_no2[["date.utc", "location", "parameter", "value"]]

## Load the Particulate matter data
air_quality_pm25 = pd.read_csv("data/air_quality_pm25_long.csv", parse_dates=True)
air_quality_pm25 = air_quality_pm25[["date.utc", "location", "parameter", "value"]]

Concatenating the Datasets

Next, we will combine the measurements of Nitrate and Particulate matter into a single table using the concat function.

## Concatenate the two dataframes
air_quality = pd.concat([air_quality_pm25, air_quality_no2], axis=0)

Merge Tables Using a Common Identifier

We will then add the station coordinates to the measurements table using the merge function. We will perform a left join on the location column.

## Load the stations coordinates data
stations_coord = pd.read_csv("data/air_quality_stations.csv")

## Merge the air_quality and stations_coord dataframes
air_quality = pd.merge(air_quality, stations_coord, how="left", on="location")

Add Parameters' Full Description and Name

Lastly, we will add the parameters' full description and name to the measurements table. We perform a left join on the parameter and id columns.

## Load the air quality parameters data
air_quality_parameters = pd.read_csv("data/air_quality_parameters.csv")

## Merge the air_quality and air_quality_parameters dataframes
air_quality = pd.merge(air_quality, air_quality_parameters, how='left', left_on='parameter', right_on='id')

Summary

In this lab, we learned how to combine multiple tables in pandas. We used the concat function to concatenate tables and the merge function to join tables using a common identifier. These operations are crucial when working with multiple data sources that need to be combined into a single, coherent dataset for analysis.

Other Python Tutorials you may like