根据组进行插值

时间:2019-05-29 16:56:08

标签: python pandas scikit-learn scipy

我试图基于test_data数据帧上的输入数据(test_data_Inputs)进行插值。现在设置的方式是由Peril完成,所以我首先创建了一个仅包含火灾危险的数据框(请参见下文),然后对该特定危险组执行插值:

目标是在test_data_inputs中具有一列同时具有“风险类型”和“因子”的列。我遇到的问题之一是test_data_input中的保险金额与test_data数据帧内的保险金额完全匹配的情况。无论是否完美匹配,它仍然会插值。

fire_peril_test=test_data[test_data['Peril Type'=='Fire']]
from scipy import interpolate
x=fire_peril_test['Amount of Insurance']
x=fire_peril_test['Amount Of Insurance']
y=_fire_peril_test['Factor']
y=fire_peril_test['Factor']
f=interpolate.interp1d(x,y)
xnew=test_data_Inputs["Amount of Insurance"]
ynew=f(xnew)


test_data_Inputs=pd.DataFrame({'Amount of Insurance':[320000,330000,340000]})
test_data=pd.DataFrame({'Amount of Insurance':[300000,350000,400000,300000,350000,400000],'Peril Type':['Fire','Fire','Fire','Water','Water','Water'],'Factor':[.10,.20,.35,.20,.30,.40]})

感谢所有帮助。

1 个答案:

答案 0 :(得分:0)

amount_of_insurance=pd.DataFrame()
df['Amount of Insurance']=pd.melt(df['Amount of Insurance'],id_vars=['Amount Of Insurance'],var_name='Peril Type',value_name='Factor')

for peril in df['Amount of Insurance']['Peril Type'].unique():
    #peril='Fire'
    x=df['Amount of Insurance']['Amount Of Insurance'][df['Amount of Insurance']['Peril Type']==str(peril)]
    y=df['Amount of Insurance']['Factor'][df['Amount of Insurance']['Peril Type']==str(peril)]
    f=interpolate.interp1d(x,y)
    xnew=data_for_rater[['Enter Amount of Insurance']]
    ynew=f(xnew)
    append_frame=data_for_rater[['Group','Enter Amount of Insurance']]
    append_frame['Peril Type']=str(peril)
    append_frame['Factor']=ynew
    amount_of_insurance=amount_of_insurance.append(append_frame)

使用我的实际数据的解决方案。我几乎融化了数据,以便能够遍历唯一的Peril Types。如果你们有其他选择,请告诉我...