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

Last Updated : 21 Aug, 2024

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. **Pandasis 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:

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:

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.

Python `

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.