Reputation: 27
I have the following dataframe:
df = pd.DataFrame(
{
"A": [1, 4.5, 4.6, 4.7, 5.6, 6.8],
"B": [0.25, np.nan, np.nan, 4, 12.2, 14.4],
}
)
pd
A B
0 1.0 0.25
1 4.5 NaN
2 4.6 NaN
3 4.7 4.00
4 5.6 12.20
5 6.8 14.40
I would like to interpolate the missing values of column B weighted by values from column A.
df["B"][1] = ((df["B"][3]-df["B"][0])/(df["A"][3]-df["A"][0]))*(df["A"][1]-df["A"][0])+df["B"][0] --> 3.797297
df["B"][2] = ((df["B"][3]-df["B"][0])/(df["A"][3]-df["A"][0]))*(df["A"][2]-df["A"][0])+df["B"][0] --> 3.898649
Is there a way to do it using the interpolation function?
Thanks. Paolo.
Upvotes: 2
Views: 914
Reputation: 150745
Try interpolate
:
# interpolate with option `method='index'` uses index as scale
# so we set `A` as index first
df['B'] = df.set_index('A')['B'].interpolate('index').values
Output:
A B
0 1.0 0.250000
1 4.5 3.797297
2 4.6 3.898649
3 4.7 4.000000
4 5.6 12.200000
5 6.8 14.400000
Upvotes: 1