How to Fill Missing Values in Pandas Using Conditional Column Matching
This article walks through a real‑world Pandas problem where missing values in one column are filled based on another column, presenting both a simple one‑liner solution and a more robust function‑based approach with full code examples.
1. Introduction
Hello, I am a Python enthusiast. In a Python community a user asked a Pandas question about filling missing values based on another column. Below is the discussion screenshot.
The original data looks like this:
The goal is to fill one column with values from another column when certain conditions are met.
2. Solution
One contributor posted a quick answer (screenshot below):
Simple one‑liner: df["col1"].fillna(df["col2"]) A more robust solution using a helper function and apply:
import pandas as pd
file = ""
output_filename = ""
data1 = pd.read_excel(file, sheet_name='Sheet1', dtype={'eventdate': 'datetime64[ns]', 'u1': 'datetime64[ns]'})
d2 = pd.read_excel(file, sheet_name='Sheet2', dtype={'f1': 'datetime64[ns]', 'f2': 'datetime64[ns]'})
def match_description(s, df, compare_col, value_col):
"""Return the first value of value_col where compare_col equals s, or None."""
compare_data = df[df[compare_col] == s].copy()
if compare_data.empty:
return None
return compare_data[value_col].values[0]
# Apply based on exact time match
d2['gbvibforwardrms'] = d2['f1'].apply(match_description, args=(data1, 'u1', 'gbvibforwardrms'))
# Apply based on date match
d2['gbvibforwardrms1'] = d2['f2'].apply(match_description, args=(data1, 'eventdate', 'gbvibforwardrms'))
# Combine results, fill missing values, and replace remaining NaNs with empty string
d2['c'] = d2['gbvibforwardrms'].fillna(d2['gbvibforwardrms1']).fillna('')
# Save the result
d2['c'].to_excel(output_filename, sheet_name='data3')The code successfully resolves the missing‑value issue.
3. Additional Tips
Several common pandas data‑analysis tricks were also shared:
Cheat‑sheet for quick reference: Pandas cheat‑sheet
4. Conclusion
This article presented a specific Pandas problem, offered both a concise one‑liner and a flexible function‑based solution, and provided additional data‑analysis tips to help readers handle column‑wise missing‑value filling effectively.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
