Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Left Join | Joining Data
Data Manipulation using pandas
course content

Contenido del Curso

Data Manipulation using pandas

Data Manipulation using pandas

1. Preprocessing Data: Part I
2. Preprocessing Data: Part II
3. Grouping Data
4. Aggregating and Visualizing Data
5. Joining Data

bookLeft Join

Let's start with left and right joins. What does left join mean? Left join returns all the records from the left table, and matching records from the right table. If not all the records were matched, than remaining fields would be filled with NAs.

Implementation in Python

All types of joins are implemented in pandas with the df_left.merge(df_right, on = 'column', how = 'method') function. There df_left is the left table, df_right is the right one, on - key field (column) that will be used for comparison, how - type of join.

For instance, let's perform a left join of two dataframes. Column with unique values in each dataframe is id. To perform a left join, set the how = 'left' parameter.

12345678910
# Importing library import pandas as pd # Loading data data1 = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f2947b09-5f0d-4ad9-992f-ec0b87cd4b3f/section5/data1.csv') data2 = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f2947b09-5f0d-4ad9-992f-ec0b87cd4b3f/section5/data2.csv') # Perform a left join df_res = data1.merge(data2, on = 'id', how = 'left') print(df_res)
copy

As you can see, all the records from the data1 table were left, and only matching record from the data2 table were added.

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 5. Capítulo 2
We're sorry to hear that something went wrong. What happened?
some-alt