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