Search code examples
pythonpandasnested-if

Apply nested if statement on df


The following code must perform a few steps:

  1. if column 'wholesale data' usage >= column 'mbs' than
  2. Subtract column 'wholesale data' - 'mbs'
  3. Multiply the difference based on the following table

    • 32 - 0.56
    • 64 - 0.5
    • 128 - 0.4
    • 256 - 0.4
    • 512 - 0.3
    • 1024 - 0.3
  4. For all other possible option - 0

I have got the following code

if df['Wholesale Data Usage'] >= df['mbs']:
    if df['mbs'] == "32":
        df['Additional Charge'] = (df['Wholesale Data Usage'] - df[
        'mbs']) * 0.5
    elif df['mbs'] == "64":
        df['Additional Charge'] = (df['Wholesale Data Usage'] - df[
        'mbs']) * 0.5
    elif df['mbs'] == "128":
        df['Additional Charge'] = (df['Wholesale Data Usage'] - df[
        'mbs']) * 0.4
    elif df['mbs'] == "256":
        df['Additional Charge'] = (df['Wholesale Data Usage'] - df[
        'mbs']) * 0.4
    elif df['mbs'] == "512":
        df['Additional Charge'] = (df['Wholesale Data Usage'] - df[
        'mbs']) * 0.3
    elif df['mbs'] == "1024":
        df['Additional Charge'] = (df['Wholesale Data Usage'] - df[
        'mbs']) * 0.3
    else:
        df['Additional Charge'] = 0
else:
    df['Additional Charge'] = 0

Not sure what I am doing wrong.


Solution

  • Youve got code so hard to read its hard to debug. Coding style will help you to see your own errors I think. What if you wrote it like this?

    mbs = df['mbs']
    wdu = df['Wholesale Data Usage']
    
    ac = 0
    if wdu >= mbs and mbs == "32"   : ac = (wdu - mbs) * 0.56
    if wdu >= mbs and mbs == "64"   : ac = (wdu - mbs) * 0.5
    if wdu >= mbs and mbs == "128"  : ac = (wdu - mbs) * 0.4
    if wdu >= mbs and mbs == "256"  : ac = (wdu - mbs) * 0.4
    if wdu >= mbs and mbs == "512"  : ac = (wdu - mbs) * 0.3
    if wdu >= mbs and mbs == "1024" : ac = (wdu - mbs) * 0.3
    
    df['Additional Charge'] = ac
    

    That being said you have a type problem. I the first equality mbs=="32" you are comparing as if you have a string. Then in the equation you try to add the values as if they contain a int -- which isn't going to work. Lets make the types consitent.

    mbs = float(df['mbs'])
    wdu = float(df['Wholesale Data Usage'])
    
    ac = 0
    if wdu >= mbs and mbs == 32   : ac = (wdu - mbs) * 0.56
    if wdu >= mbs and mbs == 64   : ac = (wdu - mbs) * 0.5
    if wdu >= mbs and mbs == 128  : ac = (wdu - mbs) * 0.4
    if wdu >= mbs and mbs == 256  : ac = (wdu - mbs) * 0.4
    if wdu >= mbs and mbs == 512  : ac = (wdu - mbs) * 0.3
    if wdu >= mbs and mbs == 1024 : ac = (wdu - mbs) * 0.3
    
    df['Additional Charge'] = str(ac) #<- you need to know what the expected type is here.