We can use different functions to combine different dataframes including: concat
, append
, merge
, and join
.
The following graph shows all the different types of joins available:
In this case, we have two dataframes: x and y.
For an inner join, only the shaded area will be returned.
In this case, we have two dataframes: x and y.
This takes all the data from x and y
In this case, we have two dataframes: x and y.
This takes everything from the left as a point of reference and matches everything onto the left dataframe
In this case, we have two dataframes: x and y.
This takes everything from the right as a point of reference and matches everything onto the right dataframe
Let us say that we have the following dataframes:
Other methods of merging two dataframes include join
and concat
.
To keep things simple, we will not do those.
Let us imagine the following datasets:
raw_data_1 = { 'subject_id': ['1', '2', '3', '4', '5'],
'first_name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
'last_name': ['Anderson', 'Ackerman', 'Ali', 'Aoni', 'Atiches']}
raw_data_2 = { 'subject_id': ['4', '5', '6', '7', '8'],
'first_name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
'last_name': ['Bonder', 'Black', 'Balwner', 'Brice', 'Btisan']}
raw_data_3 = { 'subject_id': ['1', '2', '3', '4', '5', '7', '8', '9', '10', '11'],
'test_grade': [51, 15, 15, 61, 16, 14, 15, 1, 61, 16]}
# Merge the DataFrames
merged_df = pd.merge(df1, df2, on=['subject_id', 'first_name', 'last_name'], how='outer')
merged_df
subject_id first_name last_name
0 1 Alex Anderson
1 2 Amy Ackerman
2 3 Allen Ali
3 4 Alice Aoni
4 4 Billy Bonder
5 5 Ayoung Atiches
6 5 Brian Black
7 6 Bran Balwner
8 7 Bryce Brice
9 8 Betty Btisan
subject_id first_name last_name test_grade
0 1 Alex Anderson 51.0
1 2 Amy Ackerman 15.0
2 3 Allen Ali 15.0
3 4 Alice Aoni 61.0
4 4 Billy Bonder 61.0
5 5 Ayoung Atiches 16.0
6 5 Brian Black 16.0
7 6 Bran Balwner NaN
8 7 Bryce Brice 14.0
9 8 Betty Btisan 15.0
subject_id first_name_x last_name_x first_name_y last_name_y
0 4 Alice Aoni Billy Bonder
1 5 Ayoung Atiches Brian Black
Popescu (JCU): Lecture 11