Home > Software design >  Python optimization of loop in data frame with max and min values
Python optimization of loop in data frame with max and min values

Time:03-16

I have question how can I optimize my code, in fact only the loops. I use to calculate solutions maximum of two rows, or sometimes max of row and number.

I tried to change my code using .loc and .clip but when it is about max or min which shows up multiple times I have some troubles with logical expressions.

That it was looking at the begining:

def Calc(row):
    if row['Forecast'] == 0:
        return max(row['Qty'],0)
    elif row['def'] == 1:
         return 0
    elif row['def']  == 0:
        return round(max(row['Qty'] - ( max(row['Forecast_total']*14,(row['Qty_12m_1'] row['Qty_12m_2'])) * max(1, (row['Total']/row['Forecast'])/54)),0 ))

df['Calc'] = df.apply(Calc, axis=1)

I menaged to change it using functions that I pointed but I have a problem how to write this max(max())

df.loc[(combined_sf2['Forecast'] == 0),'Calc'] = df.clip(0,None)
df.loc[(combined_sf2['def'] == 1),'Calc'] = 0
df.loc[(combined_sf2['def'] == 0),'Calc'] = round(max(df['Qty']- (max(df['Forecast_total']
                                                                      *14,(df['Qty_12m_1'] df['Qty_12m_2']))
                                                                      *max(1, (df['Total']/df['Forecast'])/54)),0))

First two functions are working, the last one doesn't.

id  Forecast    def Calc        Qty Forecast_total  Qty_12m_1   Qty_12m_2   Total
31551   0       0   0            2        0             0       0             95
27412   0,1     0   1            3        0,1           11      0              7
23995   0,1     0   0            4        0             1       0              7
27411   5,527   1   0,036186    60       0,2            64      0             183
28902   5,527   0   0,963814    33       5,327          277     0             183
23954   5,527   0   0            6        0             6       0             183
23994   5,527   0   0            8        0             0       0             183
31549   5,527   0   0            6        0             1       0             183
31550   5,527   0   0            6        0             10      0             183

CodePudding user response:

Use numpy.select and instead max use numpy.maximum:

m1 = df['Forecast'] == 0
m2 = df['def'] == 1
m3 = df['def'] == 0

s1 = df['Qty'].clip(lower=0)
s3 = round(np.maximum(df['Qty'] - (np.maximum(df['Forecast_total']*14,(df['Qty_12m_1'] df['Qty_12m_2'])) * np.maximum(1, (df['Total']/df['Forecast'])/54)),0 ))

df['Calc2'] = np.select([m1, m2, m3], [s1, 0, s3], default=None)
  • Related