简体   繁体   中英

More Succinct Elif Statement

Is there a way to more efficiently code this? I tried np.where but there are so many conditions and they're so complex I couldn't get it to work properly. I know there has to be a faster way then a massive elif statement, but It's the only way I could dump all of my conditions in there. I'm running through about 1 million lines of excel so if there are other faster options I'd like to hear them!

Here is the code:

def pol_cost(x):
    if ( 9500000> x.CRV >= 50 ):
        if (x.MDI > MDI_var):
            if (x.CI < 40 ):
               return x.CRV
            elif (60 <= x.CI <80):
               return x.CRV*((100-x.CI)/(100-var_var))**x.Cost_Escalation_N
        else:
            return 0
    else: 
         return 0
    df1['Policy_Cost_Y1'] = df1.apply(pol_cost, axis = 1)

@chepner's simplificiation of your conditional statements allows you to then vectorize the entire operation to

a = x.CI < 40
b = numpy.logical_and(
    60 <= x.CI,
    x.CI < 80,
)
c = numpy.logical_or(
    numpy.logical_and(
        9500000 > x.CRV,
        x.CRV >= 50
    ),
    x.MDI <= MDI_var,
)

tmp = numpy.zeros_like(x.CRV)
tmp[a] = x.CRV[a]
tmp[b] = x.CRV[b] * ((100-x.CI[b])/(100 - var_var))**x.Cost_Escalation_N[b],
tmp[c] = 0
df1['Policy_Cost_Y1'] = tmp

A useful idiom is to handle the clause the returns immediately; then you don't need an else or elif .

def pos_cost(x):
    if not (9500000  > x.CRV >= 50):
        return 0

    if x.MDI <= MDI_var:
        return 0

    if x.CI < 40:
        return x.CRV
    elif 60 < x.CI < 80:
        return x.CRV * ((100-x.CI)/(100 - var_var))**x.Cost_Escalation_N

    df1['Policy_Cost_Y1'] = df1.apply(pol_cost, axis=1)

Note that since the first two if statements have the same result, you can combine them into a single statement:

if not (9500000 > x.CRV >= 50) or (x.MDI <= MDI_var):
    return 0

or after a little manipulation:

if 9500000 <= x.CRV or x.CRV < 50 or x.MDI <= MDI_var:

You can combine the two first conditions with and and test the inverse of that. Then go with elif statements.

if not (9500000 > x.CRV >= 50 and x.MDI > MDI_var):
    return 0

elif x.CI < 40 :
    return x.CRV

elif 60 <= x.CI <80:
    return x.CRV*((100-x.CI)/(100-var_var))**x.Cost_Escalation_N

else:
    df1['Policy_Cost_Y1'] = df1.apply(pol_cost, axis = 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM