Performing a left join between two DataFrames in Pandas
3. Left Join Merge
Write a Pandas program that performs a left join of two DataFrames.
This exercise demonstrates a left join of two DataFrames, including all rows from the left DataFrame and matching rows from the right.
Sample Solution :
Code :
Output:
ID Name Age 0 1 Selena NaN 1 2 Annabel 25.0 2 3 Caeso 30.0
Explanation:
- Created two DataFrames df1 and df2.
- Used pd.merge() with how='left' to perform a left join.
- The result includes all rows from the left DataFrame and only the matching rows from the right DataFrame.
For more Practice: Solve these Related Problems:
- Write a Pandas program to perform a left join on two DataFrames and replace missing values from the right DataFrame with the left’s column median.
- Write a Pandas program to perform a left join on two DataFrames and then verify that all rows from the left DataFrame are preserved.
- Write a Pandas program to perform a left join on two DataFrames and sort the resulting DataFrame based on a column from the right DataFrame.
- Write a Pandas program to perform a left join on two DataFrames and then group the merged result by a column from the left DataFrame.
Go to:
Previous: Outer Join Merge.
Next: Right Join Merge.
Python-Pandas Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.