Table of Contents
Pandas is a powerful data manipulation tool in Python that allows users to easily handle missing data, represented as NaN (Not a Number) values. One common task in data analysis is to fill these NaN values with meaningful data. Pandas provides a convenient method to do so by using the “fillna” function, which allows users to specify a column from which the NaN values will be filled. This means that if one column in a dataset has missing values, the values from another column can be used to replace them. This feature of Pandas makes it easy for users to clean and prepare their data for analysis by ensuring that all values are accounted for.
Pandas: Fill NaN Values with Values from Another Column
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 columndf['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 .
Additional Resources
The following tutorials explain how to perform other common operations in pandas:
Cite this article
stats writer (2024). How can I use Pandas to fill NaN values in one column with values from another column?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-pandas-to-fill-nan-values-in-one-column-with-values-from-another-column/
stats writer. "How can I use Pandas to fill NaN values in one column with values from another column?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-pandas-to-fill-nan-values-in-one-column-with-values-from-another-column/.
stats writer. "How can I use Pandas to fill NaN values in one column with values from another column?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-pandas-to-fill-nan-values-in-one-column-with-values-from-another-column/.
stats writer (2024) 'How can I use Pandas to fill NaN values in one column with values from another column?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-pandas-to-fill-nan-values-in-one-column-with-values-from-another-column/.
[1] stats writer, "How can I use Pandas to fill NaN values in one column with values from another column?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use Pandas to fill NaN values in one column with values from another column?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
