Open In App

Python | Pandas DataFrame.fillna() to replace Null values in dataframe

Last Updated : 21 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Sometimes csv file has null values, which are later displayed as NaN in Data Frame. Just like the pandas dropna() method manages and remove Null values from a data frame, fillna() manages and let the user replace NaN values with some value of their own. 

Pandas DataFrame.fillna() Syntax

Syntax: DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)

Parameters: 

  • value : Static, dictionary, array, series or dataframe to fill instead of NaN.
  • method : Method is used if user doesn’t pass any value. Pandas have different methods like bfill, backfill, or ffill which fills the place with value in the Forward index or Previous/Back respectively.
  • axis: axis takes int or string value for rows/columns. Input can be 0 or 1 for Integer and ‘index’ or ‘columns’ for String
  • inplace: It is a boolean which makes the changes in data frame itself if True.
  • limit : This is an integer value which specifies maximum number of consecutive forward/backward NaN value fills.
  • downcast : It takes a dict which specifies what dtype to downcast to which one. Like Float64 to int64.
  • **kwargs : Any other Keyword arguments

Python Pandas DataFrame.fillna() to Replace Null values in Dataframe

Below are the ways by which we can replace null values in Dataframe in Python:

  • Replace NaN Values with String | Pandas
  • Before Replacing
    • After Replacing
  • Using method parameter
  • Using Limit

Pandas: How to Replace NaN Values with String

Example 1: Replacing NaN values with a Static value Before Replacing

In this example, we are using pandas library to import the “nba.csv” file and create a DataFrame named “nba” containing the data from the CSV file, which is then displayed using the nba variable.

Python3




# importing pandas module
import pandas as pd
  
# making data frame from csv file
nba = pd.read_csv("nba.csv")
  
nba


Output

Example 2: Replacing NaN values with a Static value After replacing

In the following example, all the null values in College column has been replaced with “No college” string. Firstly, the data frame is imported from CSV and then College column is selected and fillna() method is used on it. 

Python




# importing pandas module
import pandas as pd
  
# making data frame from csv file
nba = pd.read_csv("nba.csv")
  
# replacing na values in college with No college
nba["College"].fillna("No College", inplace = True)
  
nba


Output:

Replacing Null Values Using method Parameter

In the following example, method is set as ffill and hence the value in the same column replaces the null value. In this case Georgia State replaced null value in college column of row 4 and 5. Similarly, bfill, backfill and pad methods can also be used. 

Python




# importing pandas module
import pandas as pd
  
# making data frame from csv file
nba = pd.read_csv("nba.csv")
  
# replacing na values in college with No college
nba["College"].fillna( method ='ffill', inplace = True)
  
nba


Output

Replacing Null Values Using Limit

In this example, a limit of 1 is set in the fillna() method to check if the function stops replacing after one successful replacement of NaN value or not. 

Python




# importing pandas module
import pandas as pd
  
# making data frame from csv file
nba = pd.read_csv("nba.csv")
  
# replacing na values in college with No college
nba["College"].fillna( method ='ffill', limit = 1, inplace = True)
  
nba


Output:

As shown in the output, The college column of 4th row was replaced but 5th one wasn’t since the limit was set 1.



Previous Article
Next Article

Similar Reads

Python | Pandas Index.fillna()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.Pandas Index.fillna() function fill NA/NaN values with the specified value. It only takes a scalar value to be filled for all the missing
2 min read
Python | Pandas TimedeltaIndex.fillna
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.fillna() function fill all the missing values in the given TimedeltaIndex object with the specified value. Syntax
2 min read
Fillna in multiple columns in place in Python Pandas
In this article, we are going to write Python script to fill multiple columns in place in Python using pandas library. A data frame is a 2D data structure that can be stored in CSV, Excel, .dB, SQL formats. We will be using Pandas Library of python to fill the missing values in Data Frame. Fillna in multiple columns inplace First creating a Dataset
3 min read
Pandas Series.fillna() Method
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series fillna() Syntax Pandas Series.fillna() function is used to fill Pandas NA/NaN v
3 min read
Replace values of a DataFrame with the value of another DataFrame in Pandas
In this article, we will learn how we can replace values of a DataFrame with the value of another DataFrame using pandas. It can be done using the DataFrame.replace() method. It is used to replace a regex, string, list, series, number, dictionary, etc. from a DataFrame, Values of the DataFrame method are get replaced with another value dynamically.
4 min read
Replace values in Pandas dataframe using regex
While working with large sets of data, it often contains text data and in many cases, those texts are not pretty at all. The text is often in very messier form and we need to clean those data before we can do anything meaningful with that text data. Mostly the text corpus is so large that we cannot manually list out all the texts that we want to re
4 min read
Replace all the NaN values with Zero's in a column of a Pandas dataframe
Replacing the NaN or the null values in a dataframe can be easily performed using a single line DataFrame.fillna() and DataFrame.replace() method. We will discuss these methods along with an example demonstrating how to use it. DataFrame.fillna(): This method is used to fill null or null values with a specific value. Syntax: DataFrame.fillna(self,
3 min read
Replace negative values with latest preceding positive value in Pandas DataFrame
In this article, we will discuss how to replace the negative value in Pandas DataFrame Column with the latest preceding positive value. While doing this there may arise two situations - Value remains unmodified if no proceeding positive value existsValue update to 0 if no proceeding positive value exists Let's discuss these cases in detail. Case 1:
3 min read
Replace NaN Values with Zeros in Pandas DataFrame
NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float. NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to get the desired results.  [caption width="800"] [/
4 min read
Python | Pandas Series.str.replace() to replace text in a series
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier. Pandas Series.str.replace() method works like Python .replace() method only, but it works on Series too. Before calling .replace() on a Panda
5 min read