首页 > 解决方案 > python - interpolation in pandas

问题描述

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?

标签: pythonpandasinterpolation

解决方案


使用DataFrame.apply而不是Series.apply

df['interpolate2'] = \
    df.apply(lambda row: interpolate_iv_my(x, row['new'], row['x_']), axis=1)
print(df)

# Output:
   30  40  50  60  x_                   new interpolate2
0 -23 -30 -40 -45  35  [-23, -30, -40, -45]        -26.5
1  12  20  25  34  52      [12, 20, 25, 34]         26.8
2 -12 -21 -26 -29  33  [-12, -21, -26, -29]        -14.7
3  10  15  19  25  33      [10, 15, 19, 25]         11.5
4 -23 -33 -39 -53  52  [-23, -33, -39, -53]        -41.8
5  12  22  32  67  42      [12, 22, 32, 67]         24.0
6 -32 -40 -45 -55  35  [-32, -40, -45, -55]        -36.0
7  15  25  35  45  35      [15, 25, 35, 45]         20.0
8 -20 -22 -32 -42  42  [-20, -22, -32, -42]        -24.0
9  10  12  18  19  42      [10, 12, 18, 19]         13.2

推荐阅读