【问题标题】:Interpolate based on group基于组插值
【发布时间】:2019-10-15 08:20:32
【问题描述】:

我正在尝试根据 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]})

感谢所有的帮助。

【问题讨论】:

  • 在数据框上执行groupby 并进行插值
  • 不知道如果我的输入数据是一个单独的数据框会如何工作

标签: python pandas scikit-learn scipy


【解决方案1】:
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)

我的实际数据解决方案。我几乎融合了数据,以便能够遍历独特的危险类型。如果你们有其他选择,请告诉我...

【讨论】:

    猜你喜欢
    • 2023-03-09
    • 2011-06-10
    • 2016-12-14
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    相关资源
    最近更新 更多