Signup/Sign In
PUBLISHED ON: MARCH 18, 2021

Pandas DataFrame interpolate() Method

In this tutorial, we will learn the Python pandas DataFrame.interpolate() method. This method fills NaN values using an interpolation method. The method='linear' is supported for DataFrame with a MultiIndex. When this method applied on the DataFrame, it returns the Series or DataFrame by filling the null values. It returns None if inplace=True.

The below shows the syntax of the DataFrame.interpolate() method.

Syntax

DataFrame.interpolate(method='linear', axis=0, limit=None, inplace=False, limit_direction=None, limit_area=None, downcast=None, **kwargs)

Parameters

method: str, default ‘linear’. The other methods are: {linear, time, index, values, pad, nearest, zero, slinear, quadratic, cubic, spline, barycentric, polynomial, krogh, piecewise_polynomial, spline, pchip, akima, cubicspline,from_derivatives}.

axis:'0' represents the index and '1' represents the columns and the default is None. It represents the in which axis to interpolate along.

limit: It represents the int, which is optional but must be greater than 0. It indicates the maximum number of consecutive NaNs to fill.

inplace: It represents the bool(True or False), and the default is False.

limit_direction: It includes ‘forward’, ‘backward’, ‘both’ which is optional. It represents the consecutive null values to be filled in the specified direction.

Example: Interpolate the missing values using DataFrame.interpolate() Method

Using the linear method the DataFrame.interpolate() method fills the null values by ignoring the index and treat the values as equally spaced. See the below example.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating the DataFrame
df = pd.DataFrame([(0.0, np.nan, -1.0, 1.0),(np.nan, 2.0, np.nan, np.nan),(2.0, 3.0, np.nan, 9.0),],columns=list('abcd'))
print(df)
print("-----Filling the null values using the linear method-----")
print(df.interpolate(method='linear', limit_direction='forward'))


a b c d
0 0.0 NaN -1.0 1.0
1 NaN 2.0 NaN NaN
2 2.0 3.0 NaN 9.0
-----Filling the null values using the linear method-----
a b c d
0 0.0 NaN -1.0 1.0
1 1.0 2.0 -1.0 5.0
2 2.0 3.0 -1.0 9.0

Example: Interpolate the missing values in the forward direction using DataFrame.interpolate() method with the polynomial method.

The DataFrame.interpolate() method fills the null values using the polynomial method. We must specify the order of the spline or polynomial when we use polynomial method. See the below example.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating the DataFrame
df = pd.DataFrame([(0.0, np.nan, -1.0, 1.0),(np.nan, 2.0, np.nan, np.nan),(2.0, 3.0, np.nan, 9.0),],columns=list('abcd'))
print(df)
print("-----Filling the null values using the polynomial method-----")
print(df['a'].interpolate(method='polynomial', limit_direction='forward',order=1))


a b c d
0 0.0 NaN -1.0 1.0
1 NaN 2.0 NaN NaN
2 2.0 3.0 NaN 9.0
-----Filling the null values using the polynomial method-----
0 0.0
1 1.0
2 2.0
Name: a, dtype: float64

Example: Set the method= pad in DataFrame.interpolate() method

we can interpolate the missing values in the forward direction using the pad method which fills the NaNs using existing values. See the below example.

#importing pandas as pd
import pandas as pd
#importing numpy as np
import numpy as np
#creating the DataFrame
df = pd.DataFrame([(0.0, np.nan, -1.0, 1.0),(np.nan, 2.0, np.nan, np.nan),(2.0, 3.0, np.nan, 9.0),],columns=list('abcd'))
print(df)
print("-----Filling the null values using the pad method-----")
print(df.interpolate(method='pad', limit_direction='forward'))


a b c d
0 0.0 NaN -1.0 1.0
1 NaN 2.0 NaN NaN
2 2.0 3.0 NaN 9.0
-----Filling the null values using the pad method-----
a b c d
0 0.0 NaN -1.0 1.0
1 0.0 2.0 -1.0 1.0
2 2.0 3.0 -1.0 9.0

Conclusion:

In this tutorial, we learned the Python pandas DataFrame.interpolate() method. We learned the syntax, parameters and by applying this method on the DataFrame we solved examples and understood the DataFrame.interpolate() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.