My code:
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()
Picture of the dataframe:
- I am trying to get a new column with interpolated value corresponding to the value in the 'x_' column. for instance for the first row for the x value of 52
The interpolated value will be
y = [-23,-30,-40,-45]
interpolate_iv_my(x,y,52)
which gives me -41.0
What's the best way to do this for all the rows (my dataframe has over 50k rows)?
Edited
ok came up with this not sure if this is the best solution
iter_list = []
for l,r in df.iterrows():
y = []
y.append(r['30'])
y.append(r['40'])
y.append(r['50'])
y.append(r['60'])
iter_list.append(interpolate_iv_my(x,y,r['x_']).tolist())
df ['interpolated'] = iter_list