w3resource

Merging DataFrames and Handling Missing Data Using fillna() in Pandas


17. Merge with Missing Data

Write a Pandas program to merge DataFrames with missing data.

In this exercise, we have merged two DataFrames and handle missing data (NaN) after the merge.

Sample Solution :

Code :

import pandas as pd

# Create two sample DataFrames with missing data
df1 = pd.DataFrame({
    'ID': [1, 2, 3],
    'Name': ['Selena', 'Annabel', 'Caeso']
})

df2 = pd.DataFrame({
    'ID': [1, 3, 4],
    'Age': [25, 22, 28]
})

# Perform an outer merge to include all rows and handle missing data
merged_df = pd.merge(df1, df2, on='ID', how='outer')

# Fill missing values with 'Unknown' for Name and 0 for Age
merged_df['Name'].fillna('Unknown', inplace=True)
merged_df['Age'].fillna(0, inplace=True)

# Output the result
print(merged_df)

Output:

   ID     Name   Age
0   1   Selena  25.0
1   2  Annabel   0.0
2   3    Caeso  22.0
3   4  Unknown  28.0

Explanation:

  • Created two DataFrames with potential missing data after the merge.
  • Used pd.merge() with how='outer' to include all rows and handle missing data.
  • Filled missing values in 'Name' with 'Unknown' and missing values in 'Age' with 0.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to merge two DataFrames with missing data and then fill missing values with the column mean.
  • Write a Pandas program to merge two DataFrames with missing data and drop rows where more than one column is null.
  • Write a Pandas program to merge two DataFrames with missing data and identify columns with the highest percentage of null values.
  • Write a Pandas program to merge two DataFrames with missing data and replace missing numeric values with a custom constant.

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.