1 回答
TA貢獻1852條經驗 獲得超1個贊
使用numpy方法解決了這個問題。此方法將值(或值)映射到條柱中,例如,年齡 27 將映射到第二個間隔(索引 1),代碼為:digitizearray
import pandas as pd
import numpy as np
data = {'5000': ['18.67','19.79','22.16','26.38','29.17'],
'7500': ['20.07','21.28','23.82','28.36','31.99'],
'10000': ['21.46', '22.76', '25.48', '30.33', '34.81']}
index = np.array([25, 30, 35, 40, 45])
transition_table = pd.DataFrame(data, index=index)
def get_value(age, sum_assured):
row = np.digitize(age, transition_table.index, right=True)
col = str(sum_assured)
return transition_table.iloc[row, :][col]
用法:
age = int(input("Enter age"))
sum_assured = input("Enter Sum Assured")
get_value(age, sum_assured)
結果:
>>Enter age 27
>>Enter Sum Assured 10000
>>'22.76'
希望這有幫助,如果有什么不清楚的地方,請寫一條評論。
添加回答
舉報
