0

I am trying to use the following function multiple times. I do not need help with binning my data as I have already figured that out. My question pertains more to looping through the same function multiple times, with a string input rather than a i = 1, 2, 3,... type input

##create rate impact bucket function
def bucket(rateimpact):  
    if rateimpact < -0.50:
        return '1: Over -50%'
    if rateimpact < -0.30:
        return '2: (-50%,-30%)'
    if rateimpact < -0.20:
        return '3: (-20%,-30%)'
    if rateimpact < -0.10: 
        return '4: (-10%,-20%)'
    if rateimpact < -0.05:
        return '5: (-10%,-5%)'
    if rateimpact < 0.0:
        return '6: (-5%,0%)'
    if rateimpact < 0.05:
        return '7: (0%,5%)'
    if rateimpact < 0.10:
        return '8: (5%,10%)'
    if rateimpact < 0.20:
        return '9: (10%, 20%)'
    if rateimpact < 0.30:
        return '10: (20%,30%)'
    if rateimpact < 0.30:
        return '11: (30%,50%)'
    else:
        return '12: Over 50%'

##create new column for rate impact bucket
bucket = policytable['Proposed_RateImpact'].apply(lambda rateimpact: 
bucket(rateimpact))
bucket = bucket.astype('category')
rateimpact = policytable['Proposed_RateImpact']

How can I apply the same function above (bucket(rateimpact)) on a different column without creating the same function all over again:

##create rate impact bucket function for scenario #1
def bucket(scenario1_rateimpact):  
    if scenario1_rateimpact < -0.50:
        return '1: Over -50%'
    if scenario1_rateimpact < -0.30:
        return '2: (-50%,-30%)'
    if scenario1_rateimpact < -0.20:
        return '3: (-30%,-20%)'
    if scenario1_rateimpact < -0.10: 
        return '4: (-20%,-10%)'
    if scenario1_rateimpact < -0.05:
        return '5: (-10%,-5%)'
    if scenario1_rateimpact < 0.0:
        return '6: (-5%,0%)'
    if scenario1_rateimpact < 0.05:
        return '7: (0%,5%)'
    if scenario1_rateimpact < 0.10:
        return '8: (5%,10%)'
    if scenario1_rateimpact < 0.20:
        return '9: (10%, 20%)'
    if scenario1_rateimpact < 0.30:
        return '10: (20%,30%)'
    if scenario1_rateimpact < 0.30:
        return '11: (30%,50%)'
    else:
        return '12: Over 50%'

##create new column for rate impact bucket
scenario1_bucket = policytable['Scenario1_RateImpact'].apply(lambda 
scenario1_rateimpact: bucket(scenario1_rateimpact))
scenario1_bucket = scenario1_bucket.astype('category')
scenario1_rateimpact = policytable['Scenario1_RateImpact']

I tried to change the apply(lambda scenario1_rateimpact: bucket(scenario1_rateimpact)) line of code to apply(lambda rateimpact: bucket(rateimpact)) in an attempt to call the original function, but I get an error if the bucket(sccenario1_rateimpact) code block is removed.

What I am hoping to do is have 1 bucket(rateimpact) function that I can apply to different columns in my dataframe, instead of 2 separate bucket functions (1 for each column). Is this possible? Maybe a loop would achieve what I am trying to do here?

AlyB
  • 1
  • 1

0 Answers0