- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pandas - Filling missing column values with mode
Mode is the value that appears the most in a set of values. Use the fillna() method and set the mode to fill missing columns with mode. At first, let us import the required libraries with their respective aliases −
import pandas as pd import numpy as np
Create a DataFrame with 2 columns. We have set the NaN values using the Numpy np.NaN −
dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Lexus', 'Mustang', 'Bentley', 'Mustang'],"Units": [100, 150, np.NaN, 80, np.NaN, np.NaN] } )
Find mode of the column values with NaN i.e, for Units columns here. Replace NaNs with the mode of the column where it is located using mode() on Units column −
dataFrame.fillna(dataFrame['Units'].mode()[0], inplace = True)
Example
Following is the complete code −
import pandas as pd import numpy as np # Create DataFrame dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Lexus', 'Mustang', 'Bentley', 'Mustang'],"Units": [100, 150, np.NaN, 80, np.NaN, np.NaN] } ) print"DataFrame ...\n",dataFrame # finding mode of the column values with NaN i.e, for Units columns here # Replace NaNs with the mode of the column where it is located dataFrame.fillna(dataFrame['Units'].mode()[0], inplace = True) print"\nUpdated Dataframe after filling NaN values with mode...\n",dataFrame
Output
This will produce the following output −
DataFrame ... Car Units 0 BMW 100.0 1 Lexus 150.0 2 Lexus NaN 3 Mustang 80.0 4 Bentley NaN 5 Mustang NaN Updated Dataframe after filling NaN values with mode... Car Units 0 BMW 100.0 1 Lexus 150.0 2 Lexus 80.0 3 Mustang 80.0 4 Bentley 80.0 5 Mustang 80.0
- Related Articles
- Python Pandas - Filling missing column values with median
- Python Pandas - Fill missing columns values (NaN) with constant values
- How to compare two DataFrames in Python Pandas with missing values
- How to convert a column with missing values to binary with 0 for missing values in R?
- Python - Remove a column with all null values in Pandas
- Python Pandas IntervalIndex - Check if an interval with missing values is empty or not
- Python - Filter Rows Based on Column Values with query function in Pandas?
- Python Pandas - Get unique values from a column
- Python Pandas - Display unique values present in each column
- Python Pandas – Find unique values from a single column
- Complete Equation by Filling Missing Operator in JavaScript
- How to replace missing values in a column with corresponding values in other column of an R data frame?
- Merge Python Pandas dataframe with a common column and set NaN for unmatched values
- How to use pandas series.fillna() to replace missing values?
- How to replace missing values with median in an R data frame column?

Advertisements