
- 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 - Fill missing columns values (NaN) with constant values
Use the fillna() method and set a constant value in it for all the missing values using the parameter value. 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] } )
Placing a constant value for the column values with NaN i.e. for Units columns here −
constVal = 200
Replace NaNs with the constant value i.e. 200 −
dataFrame['Units'].fillna(value=constVal, inplace=True)
Example
Following is the 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 # placing a constant value for the column values with NaN i.e, for Units columns here constVal = 200 # Replace NaNs with the constant value i.e 200 dataFrame['Units'].fillna(value=constVal, inplace=True) print"\nUpdated Dataframe after filling NaN values with constant values...\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 constant values... Car Units 0 BMW 100.0 1 Lexus 150.0 2 Lexus 200.0 3 Mustang 80.0 4 Bentley 200.0 5 Mustang 200.0
- Related Articles
- Python - How to fill NAN values with mean in Pandas?
- Python Pandas - Fill NaN values using an interpolation method
- Python Pandas - Fill NaN values with the specified value in an Index object
- Python Pandas - Filling missing column values with mode
- Python Pandas - Filling missing column values with median
- Python Pandas - Return Index without NaN values
- Python Pandas - Fill NaN with Linear Interpolation
- Python Pandas - Fill NaN with Polynomial Interpolation
- Python - Remove the missing (NaN) values in the DataFrame
- Replace NaN with zero and fill positive infinity values in Python
- Replace NaN with zero and fill negative infinity values in Python
- Replace infinity with large finite numbers but fill NaN values in Python
- How to compare two DataFrames in Python Pandas with missing values
- Replace NaN with zero and fill positive infinity for complex input values in Python
- Replace NaN with zero and fill negative infinity for complex input values in Python

Advertisements