1
votes

I've got a question and hope you can help me. Suppose a Pandas Dataframe who looks something like this:

enter image description here

I would like to do a linear interpolation to fill the missing nan values. While I was searching I only found examples when the values in the same column had to be interpolated, nothing cross column. Is there a way to do an interpolation between the left and right of the nan values?

Kind regards

PS: a code snipped:

import pandas as pd

data = {'A':[2,4,6,8,4],
        'B':[0,0,0,0,0],
        'C':[4,10,4,5,1]}

df = pd.DataFrame(data,columns=['A','B','C'])
2

2 Answers

2
votes

interpolate can be used on horizontal axis, but only NaN values are considered

df['B'] = np.NaN
df = df.fillna(df.interpolate, axis=1)

It gives:

   A    B   C
0  2  3.0   4
1  4  7.0  10
2  6  5.0   4
3  8  6.5   5
4  4  2.5   1
1
votes
df.interpolate(axis=1)

will interpolate along columns.