基于区间索引的Pandas DataFrame保费查询系统:如何遍历列实现匹配?
Hey there! I see you're trying to replace those repetitive if-elif statements with cleaner loops to handle all age ranges and sum assured values in your transition table. Let's break this down step by step to get your system working smoothly.
The Core Issue with Your Current Code
Right now, you're hardcoding each possible combination of age range and sum assured, which doesn't scale and leaves out most of your table's data. We can fix this by:
- Looping through the age range index of your DataFrame to find the matching row for the user's age.
- Looping through the sum assured columns to find the matching column for the user's input.
- Fetching the corresponding premium value and formatting your output correctly.
Revised Code with Loops
import pandas as pd # Your transition table data 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: ')) # Initialize variables to store matches matching_age_range = None premium = None # Loop through age ranges to find the correct row for age_range in transition_table.index: # Split the range string into start/end integers start, end = map(int, age_range.split('-')) if start <= age <= end: matching_age_range = age_range break # Loop through sum assured columns to find the correct premium if matching_age_range is not None: for col in transition_table.columns: if int(col) == sum_assured: premium = transition_table.loc[matching_age_range, col] break # Format and print the final result (with error handling) if premium is not None: print(f"A Policyholder of age {age} with a sum assured of {sum_assured} will pay a premium of {premium}") else: print("Sorry, we don't have premium data for the entered age or sum assured.")
Key Improvements Explained
- Age Range Matching: We split each index string (like
'18-25') into integers, then check if the user's age falls within that range. This works for all your existing age ranges without hardcoding each one. - Sum Assured Matching: We loop through the DataFrame's columns, convert each column name to an integer, and match it to the user's input sum assured.
- Error Handling: Added a fallback message if the user enters an age or sum assured that's not in your table (like age 46 or sum assured 15000).
- Clean Output: Used an f-string for formatting the final message, which avoids type conversion errors (no more trying to concatenate integers with strings!).
Testing the Examples
- If you enter age
18and sum assured5000, you'll get:A Policyholder of age 18 with a sum assured of 5000 will pay a premium of 18.67 - If you enter age
27and sum assured10000, you'll get:A Policyholder of age 27 with a sum assured of 10000 will pay a premium of 22.76
内容的提问来源于stack exchange,提问作者Noel
相关产品推荐
相关产品推荐

