双向插值的替代方法

3
我编写了一些代码,基于两个条件:保险金额和免赔额百分比来执行插值。我很难一次性完成插值,所以必须将过滤拆分开来。表hf包含已知数据,我正在使用它来基于其进行插值。表df包含需要根据hf进行插值开发的新数据。
目前,我的解决方法是首先基于ded_amount百分比过滤每个表,然后在一个空数据框中执行插值并在每次循环后附加。
我感觉这样效率低下,有更好的方法可以执行此操作,期待听到一些改进意见,谢谢!
以下是提供的测试数据。
import pandas as pd
from scipy import interpolate

known_data={'AOI':[80000,100000,150000,200000,300000,80000,100000,150000,200000,300000],'Ded_amount':['2%','2%','2%','2%','2%','3%','3%','3%','3%','3%'],'factor':[0.797,0.774,0.739,0.733,0.719,0.745,0.737,0.715,0.711,0.709]}
new_data={'AOI':[85000,120000,130000,250000,310000,85000,120000,130000,250000,310000],'Ded_amount':['2%','2%','2%','2%','2%','3%','3%','3%','3%','3%']}

hf=pd.DataFrame(known_data)
df=pd.DataFrame(new_data)

deduct_fact=pd.DataFrame()
for deduct in hf['Ded_amount'].unique():
    deduct_table=hf[hf['Ded_amount']==deduct]
    aoi_table=df[df['Ded_amount']==deduct]
    x=deduct_table['AOI']
    y=deduct_table['factor']
    f=interpolate.interp1d(x,y,fill_value="extrapolate")
    xnew=aoi_table[['AOI']]
    ynew=f(xnew)
    append_frame=aoi_table
    append_frame['Factor']=ynew
    deduct_fact=deduct_fact.append(append_frame)
1个回答

3

是的,有一种更高效的方法可以做到这一点,而不必创建大量的中间数据帧并将它们附加在一起。看看这段代码:

from scipy import interpolate
known_data={'AOI':[80000,100000,150000,200000,300000,80000,100000,150000,200000,300000],'Ded_amount':['2%','2%','2%','2%','2%','3%','3%','3%','3%','3%'],'factor':[0.797,0.774,0.739,0.733,0.719,0.745,0.737,0.715,0.711,0.709]}
new_data={'AOI':[85000,120000,130000,250000,310000,85000,120000,130000,250000,310000],'Ded_amount':['2%','2%','2%','2%','2%','3%','3%','3%','3%','3%']}

hf=pd.DataFrame(known_data)
df=pd.DataFrame(new_data)

# Create this column now
df['Factor'] = None

# I like specifying this explicitly; easier to debug
deduction_amounts = list(hf.Ded_amount.unique())
for deduction_amount in deduction_amounts:
    # You can index a dataframe and call a column in one line
    x, y = hf[hf['Ded_amount']==deduction_amount]['AOI'], hf[hf['Ded_amount']==deduction_amount]['factor']

    f = interpolate.interp1d(x, y, fill_value="extrapolate")

    # This is the most important bit. Lambda function on the dataframe
    df['Factor'] = df.apply(lambda x: f(x['AOI']) if x['Ded_amount']==deduction_amount else x['Factor'], axis=1)

lambda函数的工作方式是: 它通过对其他列的条件进行判断,逐行遍历“因子”列并赋值。 如果扣除金额匹配,则返回df的AOI列(即您称之为xnew的内容)的插值,否则只返回相同的东西。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接