
- 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 NaN values using an interpolation method
Use the interpolate() method to fill NaN values. Let’s say the following is our CSV file opened in Microsoft Excel with some NaN values −
Load data from a CSV file into a Pandas DataFrame −
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv")
Fill NaN values with interpolate() −
dataFrame.interpolate()
Example
Following is the code −
import pandas as pd # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv") print("DataFrame...\n",dataFrame) # fill NaN values with interpolate() res = dataFrame.interpolate() print("\nDataFrame after interpolation...\n",res)
Output
This will produce the following output −
DataFrame... Car Reg_Price Units 0 BMW 2500 100.0 1 Lexus 3500 NaN 2 Audi 2500 120.0 3 Jaguar 2000 NaN 4 Mustang 2500 110.0 DataFrame after interpolation... Car Reg_Price Units 0 BMW 2500 100.0 1 Lexus 3500 110.0 2 Audi 2500 120.0 3 Jaguar 2000 115.0 4 Mustang 2500 110.0
- Related Articles
- Python Pandas - Fill NaN with Linear Interpolation
- Python Pandas - Fill NaN with Polynomial Interpolation
- Python Pandas - Fill missing columns values (NaN) with constant values
- Python Pandas - Fill NaN values with the specified value in an Index object
- Python - How to fill NAN values with mean in Pandas?
- Python Pandas - Return Index without NaN values
- 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 does pandas series argsort handles nan values?
- How to leave nan as nan in the pandas series argsort method?
- How to replace missing values with linear interpolation method in an R vector?
- 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
- How to count the NaN values in a column in a Python Pandas DataFrame?

Advertisements