Joins
Left Join¶
The left join takes the left dataframe’s index, and then matches any records on the right dataframe and merges the two dataframes. Any left index values that do not have a matching right index will be set to null for the new columns.
Notice how the joining below has null values for C, but values for A.
#Left join df1 and df2
df3 = df1.join(df2)
print(df3)
The default is to left join, but using how='left' will get us the same results.
#Left join df1 and df2
df3 = df1.join(df2, how='left')
print(df3)
Right Join¶
The right join takes the right dataframe's index, and then matches any records on the left dataframe and merges the two dataframes. Any right index values that do not have a matching left index will be set to null for the new columns.
In this example we see that our index is A & B, and there is no values for B in columns 1 and columns 2.
#Right join df1 and df2
df3 = df1.join(df2, how='right')
print(df3)
Also you might be quick to realize that a right join is the same as just having df2 on the left calling join and using a left join. The only difference will be the order of the columns but that is only a minor detail, you can easily re-arrange the column order after the join.
#Right join df1 and df2
df3 = df2.join(df1, how='left')
print(df3)
Outer¶
The outer join takes the combination of the two indices and matches then merges the dataframes. If either a left index or a right index does not have a matching index then it will be set to null in the respective new columns.
#Outer join df1 and df2
df3 = df1.join(df2, how='outer')
print(df3)
Inner¶
The inner join takes the intersection of the two indices for matching and merging. There will not be missing values in this case because it is the intersection.
#Outer join df1 and df2
df3 = df1.join(df2, how='inner')
print(df3)