Python: How to interpolate in Pandas
Having gaps in measurements is very usual, this might happen due to many reasons, like lack of data, mistakes in data acquisition, loosing…
Having gaps in measurements is very usual, this might happen due to many reasons, like lack of data, mistakes in data acquisition, loosing measurements etc, there are various techniques to deal with this, those techniques are called interpolation and depending on the nature of your data you can choose the most efficient technique, in this article we will talk about linear interpolation which is suitable for linear data, lets see how it works!
Linear interpolation
This method is ideal for data that change in a constant rate, in the following example we create a pandas dataframe with its values being the function of y = 3x + 2 with x range from 0 to 9, this expression is a linear function.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Ten values
index = np.arange(10)
# Linear relationship: y = 3x + 2
values = 3 * index + 2
# Convert values to float to allow NaN
values = values.astype(float)
values[2] = np.nan # 3rd value is NaN
values[5] = np.nan # 6th value is NaN
# Create the Data Frame
df = pd.DataFrame(values, index=index, columns=['Values'])
df.plot()From the plot we can identify the linear nature of the data and that there are missing values

Now we will apply the linear interpolation
df_inter = df.interpolate(method='linear')
df_interReviewing the interpolated data we can see the following, 3rd and 6th position are not not but filled with data.
Values
0 2.0
1 5.0
2 8.0
3 11.0
4 14.0
5 17.0
6 20.0
7 23.0
8 26.0
9 29.0Plotting those data generates the following

Conclusion
In this example we saw how linear interpolation works and how it can help us to fill gaps in linear data! there are many more methods for other kind of data and we will explorer them in next articles!
In Plain English 🚀
Thank you for being a part of the In Plain English community! Before you go:
- Be sure to clap and follow the writer ️👏️️
- Follow us: X | LinkedIn | YouTube | Discord | Newsletter
- Visit our other platforms: CoFeed | Differ
- More content at PlainEnglish.io