亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

基于間隔索引,熊貓的數據幀執行查找?

基于間隔索引,熊貓的數據幀執行查找?

天涯盡頭無女友 2022-09-20 16:02:29
我正在嘗試構建一個簡單的系統,系統要求用戶輸入年齡,輸入保額(或他/她想要收到的金額),然后根據這些輸入,系統將告訴用戶他要支付的溢價(或金額)。到目前為止,我已經能夠讓代碼工作(在某種程度上),但是我的問題是現在迭代所有列。代碼如下:import pandas as pd#data = pd.read_csv("/Users/Noel/Desktop/Transition.csv")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']}transition_table = pd.DataFrame(data, index=['18-25','26-30','31-35','36-40','41-45'])print('Hello, welcome to Axe!')age = int(input('Please enter the age of the Policyholder: '))sum_assured = int(input('Please enter the Sum assured of the Policyholder: '))if age >= 18 and age <= 25 and sum_assured == 5000:        row0 = transition_table.iloc[0, 0]        print(row0)elif age >= 26 and age <=30 and sum_assured == 5000:        col0 = transition_table.iloc[1, 0]        print(col0)print('A Policyholder of age ' + age + ' with a sum assured of ' + sum_assured + ' will pay a premium of ' )因此,當我輸入年齡為18歲,保額為5000時,我應收到以下結果:18歲的保單持有人的保額為5000,將支付18.7的保費如果我輸入的年齡為27歲,保額為10000,我希望收到以下結果:27歲的保單持有人的保額為10000,將支付22.76的保費我想我應該使用for循環來進行迭代,但我遇到了困難。
查看完整描述

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'

希望這有幫助,如果有什么不清楚的地方,請寫一條評論。


查看完整回答
反對 回復 2022-09-20
  • 1 回答
  • 0 關注
  • 102 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號