w3resource

Pandas - Performing an outer Join to include all rows in DataFrame


Pandas: Custom Function Exercise-2 with Solution


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.

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.



Follow us on Facebook and Twitter for latest update.