13.7 C
London
Monday, July 7, 2025
HomePandas in PythonDataFrame Functions in PythonPandas: How to Fill NaN Values with Values from Another Column

Pandas: How to Fill NaN Values with Values from Another Column

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use the following syntax to replace NaN values in a column of a pandas DataFrame with the values from another column:

df['col1'] = df['col1'].fillna(df['col2'])

This particular syntax will replace any NaN values in col1 with the corresponding values in col2.

The following example shows how to use this syntax in practice.

Example: Replace Missing Values with Another Column

Suppose we have the following pandas DataFrame with some missing values:

import numpy as np
import pandas as pd

#create DataFrame with some NaN values
df = pd.DataFrame({'team1': ['Mavs', np.nan, 'Nets', 'Hawks', np.nan, 'Jazz'],
                   'team2': ['Spurs', 'Lakers', 'Kings', 'Celtics', 'Heat', 'Magic']})

#view DataFrame
df

        team1	team2
0	Mavs	Spurs
1	NaN	Lakers
2	Nets	Kings
3	Hawks	Celtics
4	NaN	Heat
5	Jazz	Magic

Notice that there are two NaN values in the team1 column.

We can use the fillna() function to fill the NaN values in the team1 column with the corresponding value in the team2 column:

#fill NaNs in team1 column with corresponding values in team2 column
df['team1'] = df['team1'].fillna(df['team2'])

#view updated DataFrame 
df

        team1	team2
0	Mavs	Spurs
1	Lakers	Lakers
2	Nets	Kings
3	Hawks	Celtics
4	Heat	Heat
5	Jazz	Magic

Notice that both of the NaN values in the team1 column were replaced with the corresponding values in the team2 column.

Note: You can find the complete online documentation for the fillna() function here.

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Count Missing Values in Pandas
How to Drop Rows with NaN Values in Pandas
How to Drop Rows that Contain a Specific Value in Pandas

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories