Pandas: Merge only the data that has the same column on both dataframes (inner join)
Write a Pandas program to join the two dataframes using the common column of both dataframes.
Test Data:
student_data1: student_id name marks 0 S1 Danniella Fenton 200 1 S2 Ryder Storey 210 2 S3 Bryce Jensen 190 3 S4 Ed Bernal 222 4 S5 Kwame Morin 199
student_data2: student_id name marks 0 S4 Scarlette Fisher 201 1 S5 Carla Williamson 200 2 S6 Dante Morse 198 3 S7 Kaiser William 219 4 S8 Madeeha Preston 201
Sample Solution:
Python Code :
Sample Output:
Original DataFrames: student_id name marks 0 S1 Danniella Fenton 200 1 S2 Ryder Storey 210 2 S3 Bryce Jensen 190 3 S4 Ed Bernal 222 4 S5 Kwame Morin 199 student_id name marks 0 S4 Scarlette Fisher 201 1 S5 Carla Williamson 200 2 S6 Dante Morse 198 3 S7 Kaiser William 219 4 S8 Madeeha Preston 201 Merged data (inner join): student_id name_x marks_x name_y marks_y 0 S4 Ed Bernal 222 Scarlette Fisher 201 1 S5 Kwame Morin 199 Carla Williamson 200
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Pandas program to join the two given dataframes along rows and merge with another dataframe along the common column id.
Next: Write a Pandas program to join the two dataframes with matching records from both sides where available.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics