Fundamentals 5 min read

How to Fill Missing Values Across Columns with Pandas: A Step‑by‑Step Guide

This article walks through a real‑world Pandas problem where missing values in one column need to be filled from another column, presenting a simple one‑liner and a full‑featured Excel‑based solution with code snippets and explanations.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Fill Missing Values Across Columns with Pandas: A Step‑by‑Step Guide

1. Introduction

Hello, I'm a Python advanced learner. In a WeChat group a user asked how to use Pandas to fill missing values in one column with values from another column.

The original data is shown below.

2. Solution

One contributor suggested a simple one‑liner: df["col1"].fillna(df[col2]) A more complete solution reads two Excel sheets, defines a helper function to match rows, applies it with apply, and writes the result back to Excel:

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 matching for two columns
d2['gbvibforwardrms'] = d2['f1'].apply(match_description, args=(data1, 'u1', 'gbvibforwardrms'))
d2['gbvibforwardrms1'] = d2['f2'].apply(match_description, args=(data1, 'eventdate', 'gbvibforwardrms'))

# Fill missing values
d2['c'] = d2['gbvibforwardrms'].fillna(d2['gbvibforwardrms1']).fillna('')

# Save result
d2['c'].to_excel(output_filename, sheet_name='data3')

This approach successfully resolves the original issue.

3. Conclusion

The article demonstrates a practical Pandas technique for filling missing values across columns, provides complete code, and acknowledges the contributors who shared their solutions.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Excel
Python Crawling & Data Mining
Written by

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!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.