pandas
  1. pandas-dataframefillna

DataFrame.fillna() - ( Pandas Data Operations and Processing )

Heading h2

Syntax

DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None)

Example

import pandas as pd

df = pd.DataFrame({'A': [1, 2, None, 4], 'B': [5, 6, 7, None]})

# filling missing values with 0
df.fillna(0, inplace=True)

print(df)

Output

     A    B
0  1.0  5.0
1  2.0  6.0
2  0.0  7.0
3  4.0  0.0

Explanation

DataFrame.fillna() is a Pandas function that replaces missing values in a DataFrame with a specified value or method. It takes in various arguments, such as the value to be used for filling missing values, the method for filling missing values, and the axis along which to fill missing values.

In the above example, we create a DataFrame with missing values and then use df.fillna() to fill the missing values with 0. We do this in place by setting inplace=True.

Use

The DataFrame.fillna() function can be used to fill missing values in a Pandas DataFrame with a specified value or method. This is useful in data cleaning and preprocessing operations, as missing data can be problematic for some machine learning algorithms.

Important Points

  • DataFrame.fillna() is a Pandas function that replaces missing values in a DataFrame with a specified value or method
  • It takes in various arguments, such as the value to be used for filling missing values, the method for filling missing values, and the axis along which to fill missing values
  • inplace parameter is used to indicate whether to replace the missing values in the existing DataFrame or to create a new one

Summary

In conclusion, the DataFrame.fillna() function in Pandas is used to fill missing values in a DataFrame with a specified value or method. It is an important data cleaning and preprocessing tool that can help to make data more suitable for machine learning algorithms. The function can take various arguments, such as the value to be used for filling missing values and the axis along which to fill missing values.

Published on: