Join Two DataFrames With Pandas Using Merge

Python
Data Preprocessing

In this code snippet we show 4 ways to merge or join two dataframes using the merge function that performs an inner join, left outer join, right outer join and full outer join.

 1|  # inner join
 2|  df = df1.merge(df2, left_on='col_A', right_on='col_B')
 3|  
 4|  # left outer join
 5|  df = df1.merge(df2, left_on='col_A', right_on='col_B', how='left')
 6|  
 7|  # right outer join
 8|  df = df1.merge(df2, left_on='col_A', right_on='col_B', how='right')
 9|  
10|  # full outer join
11|  df = df1.merge(df2, left_on='col_A', right_on='col_B', how='outer')
12|  
13|  # inner join where we want to join on col_A which is present in df1 
14|  # and df2
15|  df = df1.merge(df2, on='col_A')
1 Upvote
Tags: Join | Merge
Did you find this snippet useful?

Sign up to bookmark this in your snippet library

Normalize Windowed Time Series
Python
Data Preprocessing

Scaler | Normalize | Scale | Min-max

4
Pivoting Pandas Dataframes
Python
Data Preprocessing

Pandas

3