Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.

Examples
Filling in NaN in a Series via linear interpolation:

In [1]:
import numpy as np
import pandas as pd
In [2]:
s = pd.Series([0, 2, np.nan, 4])
s
Out[2]:
0    0.0
1    2.0
2    NaN
3    4.0
dtype: float64
In [3]:
s.interpolate()
Out[3]:
0    0.0
1    2.0
2    3.0
3    4.0
dtype: float64

Filling in NaN in a Series by padding, but filling at most two consecutive
NaN at a time.

In [4]:
s = pd.Series([np.nan, "single_one", np.nan,
               "fill_two_more", np.nan, np.nan, np.nan,
               3.71, np.nan])
s
Out[4]:
0              NaN
1       single_one
2              NaN
3    fill_two_more
4              NaN
5              NaN
6              NaN
7             3.71
8              NaN
dtype: object
In [5]:
s.interpolate(method='pad', limit=2)
Out[5]:
0              NaN
1       single_one
2       single_one
3    fill_two_more
4    fill_two_more
5    fill_two_more
6              NaN
7             3.71
8             3.71
dtype: object

Filling in NaN in a Series via polynomial interpolation or splines: Both ‘polynomial’ and ‘spline’
methods require that you also specify an order (int).

In [6]:
s = pd.Series([0, 2, np.nan, 8])
s.interpolate(method='polynomial', order=2)
Out[6]:
0    0.000000
1    2.000000
2    4.666667
3    8.000000
dtype: float64

Fill the DataFrame forward (that is, going down) along each column using linear interpolation.
Note how the last entry in column ‘p’ is interpolated differently, because there is no entry after
it to use for interpolation. Note how the first entry in column ‘q’ remains NaN, because there is no entry
before it to use for interpolation.

In [7]:
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),
                   (np.nan, 4.0, -4.0, 16.0)],
                  columns=list('pqrs'))
df
Out[7]:
p q r s
0 0.0 NaN -1.0 1.0
1 NaN 2.0 NaN NaN
2 2.0 3.0 NaN 9.0
3 NaN 4.0 -4.0 16.0
In [8]:
df.interpolate(method='linear', limit_direction='forward', axis=0)
Out[8]:
p q r s
0 0.0 NaN -1.0 1.0
1 1.0 2.0 -2.0 5.0
2 2.0 3.0 -3.0 9.0
3 2.0 4.0 -4.0 16.0

Using polynomial interpolation.

In [9]:
df['s'].interpolate(method='polynomial', order=2)
Out[9]:
0     1.0
1     4.0
2     9.0
3    16.0
Name: s, dtype: float64