0

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:

enter image description here

  • 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
user13412850
  • 509
  • 1
  • 5
  • 16
  • Does this answer your question? [Applying function with multiple arguments to create a new pandas column](https://stackoverflow.com/questions/19914937/applying-function-with-multiple-arguments-to-create-a-new-pandas-column) – Jakub Szlaur Oct 11 '21 at 15:16

1 Answers1

0

I think your interpolate_iv_my function isn't working properly.

But the correct code for applying function to all rows in a dataframe and creating new column based on those results is this:

df['interpolate'] = df.apply(lambda row: interpolate_iv_my(row, x, 52), axis=1)
Jakub Szlaur
  • 1,852
  • 10
  • 39