Interpolating missing values

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.0
1 148.0
2 196.0
3 244.0
4 292.0
dtype: 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

We can perform more complex interpolation techniques, especially with time series data. An example of this interpolation is shown in the notebook provided with this chapter. 

Next, we are going to see how we can rename axis indexes. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.9.169