Table of Contents
The fillna() function in Pandas is a powerful tool that allows users to fill missing values in their data sets. Specifically, this function can be used to target and fill missing values in specific columns. By specifying the desired columns as parameters, the fillna() function will only fill the missing values in those columns, leaving the rest of the data untouched. This provides a convenient and efficient way to handle missing data in a targeted manner. Additionally, the function offers various methods for filling the missing values, such as using a specific value or using the mean or median of the column. Overall, the fillna() function is a valuable tool for managing missing data in specific columns within a Pandas data frame.
Pandas: Use fillna() with Specific Columns
You can use the following methods with fillna() to replace NaN values in specific columns of a pandas DataFrame:
Method 1: Use fillna() with One Specific Column
df['col1'] = df['col1'].fillna(0)
Method 2: Use fillna() with Several Specific Columns
df[['col1', 'col2']] = df[['col1', 'col2']].fillna(0)
This tutorial explains how to use this function with the following pandas DataFrame:
import numpy as np import pandas as pd #create DataFrame with some NaN values df = pd.DataFrame({'rating': [np.nan, 85, np.nan, 88, 94, 90, 76, 75, 87, 86], 'points': [25, np.nan, 14, 16, 27, 20, 12, 15, 14, 19], 'assists': [5, 7, 7, np.nan, 5, 7, 6, 9, 9, 5], 'rebounds': [11, 8, 10, 6, 6, 9, 6, 10, 10, 7]}) #view DataFrame df rating points assists rebounds 0 NaN 25.0 5.0 11 1 85.0 NaN 7.0 8 2 NaN 14.0 7.0 10 3 88.0 16.0 NaN 6 4 94.0 27.0 5.0 6 5 90.0 20.0 7.0 9 6 76.0 12.0 6.0 6 7 75.0 15.0 9.0 10 8 87.0 14.0 9.0 10 9 86.0 19.0 5.0 7
Example 1: Use fillna() with One Specific Column
The following code shows how to use fillna() to replace the NaN values with zeros in just the “rating” column:
#replace NaNs with zeros in 'rating' column df['rating'] = df['rating'].fillna(0) #view DataFrame df rating points assists rebounds 0 0.0 25.0 5.0 11 1 85.0 NaN 7.0 8 2 0.0 14.0 7.0 10 3 88.0 16.0 NaN 6 4 94.0 27.0 5.0 6 5 90.0 20.0 7.0 9 6 76.0 12.0 6.0 6 7 75.0 15.0 9.0 10 8 87.0 14.0 9.0 10 9 86.0 19.0 5.0 7
Notice that the NaN values have been replaced only in the “rating” column and every other column remained untouched.
Example 2: Use fillna() with Several Specific Columns
The following code shows how to use fillna() to replace the NaN values with zeros in both the “rating” and “points” columns:
#replace NaNs with zeros in 'rating' and 'points' columns df[['rating', 'points']] = df[['rating', 'points']].fillna(0) #view DataFrame df rating points assists rebounds 0 0.0 25.0 5.0 11 1 85.0 0.0 7.0 8 2 0.0 14.0 7.0 10 3 88.0 16.0 NaN 6 4 94.0 27.0 5.0 6 5 90.0 20.0 7.0 9 6 76.0 12.0 6.0 6 7 75.0 15.0 9.0 10 8 87.0 14.0 9.0 10 9 86.0 19.0 5.0 7
Notice that the NaN values have been replaced in the “rating” and “points” columns but the other columns remain untouched.
Note: You can find the complete documentation for the pandas fillna() function .
Additional Resources
Cite this article
stats writer (2024). How can I use the fillna() function in Pandas to specifically fill missing values in certain columns?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-use-the-fillna-function-in-pandas-to-specifically-fill-missing-values-in-certain-columns/
stats writer. "How can I use the fillna() function in Pandas to specifically fill missing values in certain columns?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-use-the-fillna-function-in-pandas-to-specifically-fill-missing-values-in-certain-columns/.
stats writer. "How can I use the fillna() function in Pandas to specifically fill missing values in certain columns?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-use-the-fillna-function-in-pandas-to-specifically-fill-missing-values-in-certain-columns/.
stats writer (2024) 'How can I use the fillna() function in Pandas to specifically fill missing values in certain columns?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-use-the-fillna-function-in-pandas-to-specifically-fill-missing-values-in-certain-columns/.
[1] stats writer, "How can I use the fillna() function in Pandas to specifically fill missing values in certain columns?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I use the fillna() function in Pandas to specifically fill missing values in certain columns?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
