Conteúdo do Curso
Data Manipulation using pandas
Data Manipulation using pandas
Left 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.
# 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)
As you can see, all the records from the data1
table were left, and only matching record from the data2
table were added.
Obrigado pelo seu feedback!