March 2020
Beginner to intermediate
352 pages
8h 40m
English
The pandas library provides the interpolate() function both for the series and the dataframe. By default, it performs a linear interpolation of our missing values. Check the following example:
ser3 = pd.Series([100, np.nan, np.nan, np.nan, 292])ser3.interpolate()
And the output of the preceding code is the following:
0 100.01 148.02 196.03 244.04 292.0dtype: float64
Are you wondering how these values are calculated? Well, it is done by taking the first value before and after any sequence of the NaN values. In the preceding series, ser3, the first and the last values are 100 and 292 respectively. Hence, it calculates the next value as (292-100)/(5-1) = 48. So, the next value after 100 is 100 + 48 = 148.
Read now
Unlock full access