this is a follow up from my question here
python - interpolation on dataframe values
import pandas as pd
import numpy as np
from scipy.interpolate import interp1d
def interpolate_iv_my(x,y,newX_Value):
y_interp = interp1d(x,y)
iv = y_interp(newX_Value)
return iv
import pandas as pd
df = pd.DataFrame({'30': [-23, 12, -12, 10, -23, 12, -32, 15, -20, 10],
'40': [-30, 20, -21, 15, -33, 22, -40, 25, -22, 12],
'50': [-40, 25, -26, 19, -39, 32, -45, 35, -32, 18],
'60': [-45, 34, -29, 25, -53, 67, -55, 45, -42, 19],
})
x = [30,40,50,60]
df['x_'] = np.random.choice([35,33,42,52],10).tolist()
cols = ['30','40','50','60']
df['new'] = df[cols].to_numpy().tolist()
df['interpolate2'] = df['new'].apply(lambda y: interpolate_iv_my(x,y, df['x_']))
the problem with the code is in this line
df['interpolate2'] = df['new'].apply(lambda y: interpolate_iv_my(x,y, df['x_']))
df['x_'] sends the entire column instead just one value on that row for df['x_']. How do I fix that?