Merging DataFrames and Handling Missing Data Using fillna() in Pandas
Pandas: Custom Function Exercise-17 with Solution
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.
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics