How to Use Left Merge in Pandas for DataFrames
Use
pandas.merge() with the parameter how='left' to join two DataFrames, keeping all rows from the left DataFrame and matching rows from the right. This is useful when you want to keep all data from one table and add matching information from another.Syntax
The basic syntax for a left merge in pandas is:
pandas.merge(left, right, how='left', on=None, left_on=None, right_on=None)Explanation:
left: The left DataFrame.right: The right DataFrame to merge with.how='left': Specifies a left join, keeping all rows fromleft.on: Column name(s) to join on, must be present in both DataFrames.left_onandright_on: Use these if the join columns have different names in each DataFrame.
python
import pandas as pd merged_df = pd.merge(left_df, right_df, how='left', on='key_column')
Example
This example shows how to merge two DataFrames using a left merge on a common column called id. All rows from the left DataFrame are kept, and matching rows from the right DataFrame are added.
python
import pandas as pd # Left DataFrame left_df = pd.DataFrame({ 'id': [1, 2, 3, 4], 'name': ['Alice', 'Bob', 'Charlie', 'David'] }) # Right DataFrame right_df = pd.DataFrame({ 'id': [2, 4, 5], 'score': [88, 92, 75] }) # Left merge on 'id' merged_df = pd.merge(left_df, right_df, how='left', on='id') print(merged_df)
Output
id name score
0 1 Alice NaN
1 2 Bob 88.0
2 3 Charlie NaN
3 4 David 92.0
Common Pitfalls
Common mistakes when using left merge include:
- Not specifying
how='left', which defaults to an inner join and drops unmatched rows. - Using
onwith columns that don't exist in both DataFrames. - Forgetting that unmatched rows from the right DataFrame will have
NaNvalues in the result.
Example of a wrong and right way:
python
# Wrong: missing how='left' (defaults to inner join) import pandas as pd left_df = pd.DataFrame({'id': [1, 2]}) right_df = pd.DataFrame({'id': [2, 3]}) inner_merge = pd.merge(left_df, right_df, on='id') print('Inner merge result:') print(inner_merge) # Right: specify how='left' left_merge = pd.merge(left_df, right_df, how='left', on='id') print('\nLeft merge result:') print(left_merge)
Output
Inner merge result:
id
0 2
Left merge result:
id
0 1
1 2
Quick Reference
Tips for using left merge in pandas:
- Use
how='left'to keep all rows from the left DataFrame. - Use
onwhen the join column has the same name in both DataFrames. - Use
left_onandright_onif join columns differ. - Unmatched rows from the right DataFrame will have
NaNin the result. - Check your data types on join columns to avoid unexpected results.
Key Takeaways
Use pandas.merge() with how='left' to keep all rows from the left DataFrame and add matching rows from the right.
Specify the join column(s) with on, or left_on and right_on if names differ.
Unmatched rows from the right DataFrame will have NaN values in the merged result.
Not specifying how='left' defaults to inner join, which drops unmatched rows.
Always check that join columns exist and have compatible data types in both DataFrames.