Generation

generate functionSat, 25 Feb 2023

There is a Pandas dataframe: loan_amnt int_rate 0 5000 10.65% 1 2500 15.27% 2 2400 15.96% 3 10000 13.49% 4 3000 12.69% 5 5000 7.90% 6 7000 15.96% You need to split the values of the int_rate column into categories. Important: The int_rate column is in str data format. Use regex to remove the '%' signs at the end of strings. Then convert the values to float format. Then write a function that creates the rate_group column and adds values to it as follows: 1) if int_rate > 15.00%, rate_group should be '>15' 2) if int_rate is between 10.00% and 15.00%, then rate_group should be '10-15'

import re def rate_group(int_rate): int_rate = re.sub('%', '', int_rate) int_rate = float(int_rate) if int_rate > 15.0: return '>15' elif 10.0 < int_rate <= 15.0: return '10-15' else: return '<10' df['rate_group'] = df['int_rate'].apply(rate_group)

Questions about programming?Chat with your personal AI assistant