
- 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 with Polynomial Interpolation
To fill NaN with Polynomial Interpolation, use the interpolate() method on the Pandas series. With that, set the “method” parameter to “polynomial”.
At first, import the required libraries −
import pandas as pd import numpy as np
Create a Pandas series with some NaN values. We have set the NaN using the numpy np.nan −
d = pd.Series([10, 20, np.nan, 65, 75, 85, np.nan, 100])
Find polynomial interpolation using the method parameter of the interpolate() method −
d.interpolate(method='polynomial', order=2)
Example
Following is the code −
import pandas as pd import numpy as np # pandas series d = pd.Series([10, 20, np.nan, 65, 75, 85, np.nan, 100]) print"Series...\n",d # interpolate print"\nPolynomial Interpolation...\n",d.interpolate(method='polynomial', order=2)
Output
This will produce the following output −
Series... 0 10.0 1 20.0 2 NaN 3 65.0 4 75.0 5 85.0 6 NaN 7 100.0 dtype: float64 Polynomial Interpolation... 0 10.000000 1 20.000000 2 42.854015 3 65.000000 4 75.000000 5 85.000000 6 93.532847 7 100.000000 dtype: float64
- Related Articles
- Python Pandas - Fill NaN with Linear Interpolation
- Python Pandas - Fill NaN values using an interpolation method
- Python - How to fill NAN values with mean in Pandas?
- Python Pandas - Fill missing columns values (NaN) with constant values
- Python Pandas - Fill NaN values with the specified value in an Index object
- 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
- Python Pandas - Replace all NaN elements in a DataFrame with 0s
- Python Pandas - Return Index without NaN 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
- Replace infinity with large finite numbers and fill NaN for complex input values in Python
- Merge Python Pandas dataframe with a common column and set NaN for unmatched values
- How to leave nan as nan in the pandas series argsort method?

Advertisements