Home > Enterprise >  Pandas data frame - apply function with lambda with multiple 'if else' statements
Pandas data frame - apply function with lambda with multiple 'if else' statements

Time:09-27

I am hoping someone could help point out what I may be doing wrong in the following piece of code:

master_output['tm_override'] = master_output.apply(lambda row: row['nrec_tm_lb'].astype(str)   '-'   row['nrec_tm_ub'].astype(str) if row['det_tw_fact'].isin([4, 5]) else row['tw2Open']   dt.timedelta(hours=3).time() if (row['det_tw_fact'].isin([1, 2, 3]) and (~row['tw2Open'].isna()))
else row['tw1Open']   dt.timedelta(hours=3).time() if (row['det_tw_fact'].isin([1, 2, 3]) and (~row['tw2Open'].isna())), axis=1) 

I have a feeling that I may be doing something fundamentally silly here. The issue it seems may be coming from the last set of brackets ( ')))' ) before the 'axis=1' argument.

Thanks in advance for your help!

CodePudding user response:

Nick ODell's comment is correct. I reformatted your orignal as:

master_output['tm_override'] = (
    master_output.apply(lambda row: row['nrec_tm_lb'].astype(str)   '-'   row['nrec_tm_ub'].astype(str) 
    if row['det_tw_fact'].isin([4, 5]) 
    else row['tw2Open']   dt.timedelta(hours=3).time() 
        if (row['det_tw_fact'].isin([1, 2, 3]) and (~row['tw2Open'].isna()))
        else row['tw1Open'] 
          dt.timedelta(hours=3).time() 
            if (row['det_tw_fact'].isin([1, 2, 3]) and (~row['tw2Open'].isna())), axis=1)
    )

If you look at your last if, there is no matching else. What you are dealing is I believe is a DataFrame ? You are trying to assign values to a column, but if you only have if without else, when the if condition is not met, then there is no values to fill the column.

I don't know what values you are going to fill in the else part. But I've tried filling with ''. The syntax error goes away.

master_output['tm_override'] = (
    master_output.apply(lambda row: row['nrec_tm_lb'].astype(str)   '-'   row['nrec_tm_ub'].astype(str) 
    if row['det_tw_fact'].isin([4, 5]) 
    else row['tw2Open']   dt.timedelta(hours=3).time() 
        if (row['det_tw_fact'].isin([1, 2, 3]) and (~row['tw2Open'].isna()))
        else row['tw1Open'] 
          dt.timedelta(hours=3).time() 
            if (row['det_tw_fact'].isin([1, 2, 3]) and (~row['tw2Open'].isna()))
            else '', axis=1)
    )

What I get now is a different error because I don't have the DataFrame, but your syntax error is resoved.

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-251-72ce849871e0> in <module>
      1 master_output['tm_override'] = (
----> 2     master_output.apply(lambda row: row['nrec_tm_lb'].astype(str)   '-'   row['nrec_tm_ub'].astype(str) 
      3     if row['det_tw_fact'].isin([4, 5])
      4     else row['tw2Open']   dt.timedelta(hours=3).time()
      5         if (row['det_tw_fact'].isin([1, 2, 3]) and (~row['tw2Open'].isna()))

NameError: name 'master_output' is not defined
  • Related