Home > Software design >  Lambda function with if statement [True or False output]
Lambda function with if statement [True or False output]

Time:11-03

Could you please help me to resolve this tackle? Please find attached simple DF with 2 columns.

enter image description here

The goal of this check: if month is equal June or July then check if working_time_in_hours is greater than 40 else check if working_time_in_hours is greater than 24.

Maybe sth like that however it does not work :(:

df["over_treshold"] = df.apply(lambda l: l.working_time_in_hours >= 40 if df.month == "June" | df.month =="July"  else l.working_time_in_hours >= 24)

The output should include a bool value in new column ["over_treshold]

CodePudding user response:

Use numpy.where and instead multiple | is used Series.isin:

 df["over_treshold"] = np.where(df.month.isin(["June", "July"]), 
                                df.working_time_in_hours >= 40 , 
                                df.working_time_in_hours >= 24)

CodePudding user response:

You probably want:

df["over_treshold"] = df.apply(lambda l: l.working_time_in_hours >= 40 if df.month == "June" | df.month =="July"  else l.working_time_in_hours >= 24, axis=1)

From the documentation:

axis{0 or ‘index’, 1 or ‘columns’}, default 0 Axis along which the function is applied:

  • 0 or ‘index’: apply function to each column.
  • 1 or ‘columns’: apply function to each row.
  • Related