Pandas - Performing an outer Join to include all rows in DataFrame
2. Outer Join Merge
Write a Pandas program to perform an outer join on two DataFrames.
In this exercise, we have performed an outer join on two DataFrames to include all rows from both DataFrames, with missing values filled as NaN.
Sample Solution :
Code :
import pandas as pd
# Create two sample DataFrames
df1 = pd.DataFrame({
'ID': [1, 2, 3],
'Name': ['Selena', 'Annabel', 'Caeso']
})
df2 = pd.DataFrame({
'ID': [2, 3, 4],
'Age': [25, 30, 22]
})
# Perform an outer join on the 'ID' column
outer_joined_df = pd.merge(df1, df2, on='ID', how='outer')
# Output the result
print(outer_joined_df)
Output:
ID Name Age 0 1 Selena NaN 1 2 Annabel 25.0 2 3 Caeso 30.0 3 4 NaN 22.0
Explanation:
- Created two DataFrames df1 and df2.
- Used pd.merge() with how='outer' to perform an outer join.
- The result includes all rows from both DataFrames, filling missing values with NaN.
For more Practice: Solve these Related Problems:
- Write a Pandas program to perform an outer join on two DataFrames and fill missing values with a custom constant.
- Write a Pandas program to perform an outer join on two DataFrames that have multi-index columns and then flatten the resulting columns.
- Write a Pandas program to perform an outer join on two DataFrames and compute the sum of a specified numeric column from both sources.
- Write a Pandas program to perform an outer join on two DataFrames and add a flag column indicating the source of each row.
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.