How to Pandas fillna() with mode of column?

Given a pandas dataframe, we have to fill the NaN values with the mode of the column in it.
Submitted by Pranit Sharma, on December 06, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

While creating a DataFrame or importing a CSV file, there could be some NaN values in the cells. NaN values mean "Not a Number" which generally means that there are some missing values in the cell.

Problem statement

Given a pandas dataframe, we have to fill the NaN values with the mode of the column in it.

Mode of the column

The most frequent value which occurs in a column can be considered as a mode of that column. To get the mode of a column, we first access a column by using df['col_name'], and then we will apply the mode() method which will return the most frequent value.

Fill the NaN values with the mode of the column

To fill the Nan values with the mode of that column, we will use the fillna() method inside which we will pass the column name and also applied the mode() function with this column name inside the fill any method itself.

Let us understand with the help of an example,

Python program to fill the NaN values with the mode of the column in a Pandas dataframe

# Importing pandas
import pandas as pd

# Import numpy
import numpy as np

# Creating a dictionary
d = {
    'A':[1,2,3,4,5,6,7,8,9,10],
    'B':[1,2,3,4,5,6,7,8,9,10],
    'C':[1,1,np.nan,3,4,np.nan,4,4,5,6]
}

# Creating a dataframe
df = pd.DataFrame(d)

# Display original DataFrame
print("Original DataFrame:\n",df,"\n")

# Filling nan with mode of column c
df['C'] = df['C'].fillna(df['C'].mode()[0])

# Display result
print("Result:\n",df,"\n")

Output

The output of the above program is:

Example: How to Pandas fillna() with mode of column?

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.