I have a fairly large dataframe on which I want to
- row-wise search for an interval containing a value
- perform a linear interpolation between the two elements found at point 1 and two elements from another array
- add a column to the dataframe with the interpolated values
What I have done involves a for loop, i.e.:
Given a sample of the dataframe Fak
beta0 beta1 beta2 beta3 beta4 beta5 beta6 beta7 beta8 beta9 beta10
0 0.008665 0.061391 0.159690 0.223275 0.232535 0.251266 0.279847 0.465671 0.672253 0.914753 1.0
1 0.009121 0.064322 0.166623 0.232418 0.241945 0.261106 0.290169 0.477621 0.682283 0.916384 1.0
2 0.009491 0.066689 0.172210 0.239776 0.249516 0.269020 0.298463 0.487108 0.690031 0.917638 1.0
3 0.009733 0.068232 0.175837 0.244542 0.254418 0.274140 0.303820 0.493102 0.694703 0.918304 1.0
4 0.009860 0.069027 0.177687 0.246963 0.256906 0.276734 0.306523 0.495985 0.696696 0.918511 1.0
I have an array psi
[-12.97, -11.97, -10.97, -9.97, -8.97, -7.97, -6.97, -5.97, -4.97, -3.97, -2.97, -1.97]
I define the value I want to search in Fak
, i.e. intF = 0.16
I calculate the new dataframe with the following loop
dxlist = []
for i,Faki in Fak.iterrows():
# interpolation boundaries ID
if intF == 0.0:
ip1 = 1
elif intF == 1.0:
ip1 = -1
else:
ip1 = np.where(Faki>int(intF)/100)[0][0]
im1 = ip1-1
# coefficients
dfak = Faki[ip1] - Faki[im1]
dpsi = psi[ip1] - psi[im1]
m = dfak/dpsi
q = Faki[im1]-m*psi[im1]
# calculate
intPsi = (int(intF)/100-q)/m
intDi = 2**intPsi
dxlist.append(intDi)
dfout['newcolumn'] = dxlist
which works, but it is quite slow.
What I am missing is how to calculate the linear interpolation row-wise and use the indices on an outside array.