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. Write a function that will create the rate_group column and add 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' You can use regex
import pandas as pd import re df = pd.DataFrame({'loan_amnt': [5000, 2500, 2400, 10000, 3000, 5000, 7000], 'int_rate': ['10.65%', '15.27%', '15.96%', '13.49%', '12.69%', '7.90%', '15.96%']}) df['rate_group'] = None def rate_group(a): if a > 15.00: return '>15' elif a >= 10.00 and a <= 15.00: return '10-15' else: return '<10' df['rate_group'] = df['int_rate'].apply(lambda x: (re.sub('%', '', x))) df['rate_group'] = df['rate_group'].apply(lambda x: float(x)) df['rate_group'] = df['rate_group'].apply(rate_group) display(df)