In this tutorial, we’ll look at how to fill missing values (using fillna) in one column with values from another column of a pandas dataframe.
Using fillna() to fill values from another column
The pandas dataframe fillna()
function is used to fill missing values in a dataframe. Generally, we use it to fill a constant value for all the missing values in a column, for example, 0 or the mean/median value of the column but you can also use it to fill corresponding values from another column. The following is the syntax:
df['Col1'].fillna(df['Col2'])
Here, we apply the fillna() function on “Col1” of the dataframe df and pass the series df[‘Col2’] as an argument. The above code fills the missing values in “Col1” with the corresponding values (based on the index) from “Col2”. To modify the dataframe in-place, pass inplace=True
to the above function.
Example
Let’s look at a use case of filling missing or NA values in a column with values from another column using the above method. First, let’s create a sample dataframe to operate on.
import numpy as np import pandas as pd # dataframe of postal and permanent address df = pd.DataFrame({ 'Postal Address': ['New York', np.nan, 'London', 'Mumbai', np.nan], 'Permanent Address': ['Miami', 'Amsterdam', 'London', 'Rajkot', 'Sydney'] }) print(df)
Output:
Postal Address Permanent Address 0 New York Miami 1 NaN Amsterdam 2 London London 3 Mumbai Rajkot 4 NaN Sydney
In the above dataframe we have postal and permanent addresses (for simplicity they are just city names) of winners of an online contest.
Note that, some of the postal addresses are missing. The organizer wants to send out the prizes to all the winners. For lack of better information, the organizer decides to send out the prizes to the permanent addresses for winners where you don’t have a postal address.
To fill the missing values in the column “Postal Address” with corresponding values from the column “Permanent Address”:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
# fill missing values df['Postal Address'].fillna(df['Permanent Address'], inplace=True) print(df)
Output:
Postal Address Permanent Address 0 New York Miami 1 Amsterdam Amsterdam 2 London London 3 Mumbai Rajkot 4 Sydney Sydney
You can see that the values “Amsterdam” and “Sydney” from column “Permanent Address” are filled in the missing values at index 1 and 4 of column “Postal Address” resepectively.
For more on the pandas fillna() function, refer to its documentation. Our other related tutorials:
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having numpy version 1.18.5 and pandas version 1.0.5
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.