Python

Pandas fillna() : Replace NaN Values in the DataFrame

By ayed_amira , on 01/12/2021 - 2 minutes to read
pandas fillna

Pandas fillna() : In this tutorial we will learn how to use the fillna() function of the pandas python module to replace the NaN values of a pandas dataframe.

Introduction

The Pandas module is a python-based toolkit for data analysis that is widely used by data scientists and data analysts. It simplifies data import and data cleaning. Pandas also offers several ways to create a type of data structure called dataframe (It is a data structure that contains rows and columns).

The most frequent problem when cleaning the data is managing the case of missing values in our dataframe. There are a lot of factors for a value to be missing (for example, in a survey, a person has not filled in a field of the form).

To overcome this problem, the fillna() method in the pandas module will help us to manage these missing values. It will replace all the None or NaN values by the value of your choice.

Pandas fillna() Syntax

The syntax of the Dataframe.fillna() function is as follows:

#Pandas fillna() Syntax

DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)
  • value : scalar, dictionary, pandas Series or a DataFrame
  • method : {‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}.
  • axis : 0 or ‘index’, 1 or ‘columns’.
  • inplace: it is a boolean argument. If True, the DataFrame is modified inplace, and if False a new DataFrame with resulting contents is returned.
  • limit : takes integer or None. This is the maximum number of consecutive NaN values to forward/backward fill. This argument is used : only if method is specified.
  • downcast : can be a dictionary or None.
  • **kwargs : Any other Keyword arguments

These arguments are pretty straightforward to use. We will then give some examples to understand how to use the fillna() function.

Pandas fillna() to replace NaN values with 0

ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Comments

Leave a comment

Your comment will be revised by the site if needed.