Pandas Dataframe interpolate Tutorial : Pandas dataframe.interpolate () function is used to fill NA values in the dataframe or in series. But, this is powerful function to fill the missing values. It uses various interpolation techniques to fill the missing values than hard-coding the value.
Syntax:
DataFrame.interpolate(method=’linear’, axis=0, limit=None, inplace=False, limit_direction=’forward’, limit_area=None, downcast=None, **kwargs)
Method: - {linear, time, index, value, zero}
Axis:-0 will fill column by column and 1 will fill row by row.
Limit:-Maximum number of consecutive must be greater than 0.
Limit_direction:- {forward,backward}
Limit_area:-No fill instruction only fills Nan surrounded by valid values.
Inplace:-update the ndframe in place if possible.
Downcast:-it is dtype if possible.
Kwargs:-Keyword argument to pass on interpolating function.
Returns:-The dataframe series of same shape at Nans.
Use interpolate() function to interpolate the missing values in the backward direction using linear method and putting a limit on maximum number of consecutive Na values that could be filled.
import pandas as pd
df=pd.dataframe({“A”:[12,4,5,none,1], “B”:[none,2,54,3,none] ,”c”:[20,16,none,3,8],”D”:[14,3,none,none,6] })
df.interpolate (method=’linear’,limit_direction=’backward’,limit=1)
Output:-
In the fourth column, only one missing value has been filled we have put the limit to 1. The missing value in the last row will not fill as no row exists after that.