
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Filling missing column values with median
Median separates the higher half from the lower half of the data. Use the fillna() method and set the median to fill missing columns with median. 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": ['Lexus', 'BMW', 'Audi', 'Bentley', 'Mustang', 'Tesla'],"Units": [100, 150, np.NaN, 80, np.NaN, np.NaN] } )
Find median of the column values with NaN i.e, for Units columns here. Replace NaNs with the median of the column where it is located using median() on Units column −
dataFrame.fillna(dataFrame['Units'].median(), inplace = True)
Example
Following is the code −
import pandas as pd import numpy as np # Create DataFrame dataFrame = pd.DataFrame( { "Car": ['Lexus', 'BMW', 'Audi', 'Bentley', 'Mustang', 'Tesla'],"Units": [100, 150, np.NaN, 80, np.NaN, np.NaN] } ) print"DataFrame ...\n",dataFrame # finding median of the column values with NaN i.e, for Units columns here # Replace NaNs with the median of the column where it is located dataFrame.fillna(dataFrame['Units'].median(), inplace = True) print"\nUpdated Dataframe after filling NaN values with median...\n",dataFrame
Output
This will produce the following output −
DataFrame ... Car Units 0 Lexus 100.0 1 BMW 150.0 2 Audi NaN 3 Bentley 80.0 4 Mustang NaN 5 Tesla NaN Updated Dataframe after filling NaN values with median... Car Units 0 Lexus 100.0 1 BMW 150.0 2 Audi 100.0 3 Bentley 80.0 4 Mustang 100.0 5 Tesla 100.0
- Related Articles
- Python Pandas - Filling missing column values with mode
- Python Pandas - Fill missing columns values (NaN) with constant values
- How to replace missing values with median in an R data frame column?
- Python - Calculate the median of column values of a Pandas DataFrame
- 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 – Find unique values from a single column
- Python Pandas - Display unique values present in each column
- How to replace missing values in a column with corresponding values in other column of an R data frame?
- Complete Equation by Filling Missing Operator in JavaScript
- Merge Python Pandas dataframe with a common column and set NaN for unmatched values

Advertisements