python - Invalid syntax error when applying lambda for multiple columns processing -
i have following code should put either 0 or 1 column indicator
depending on if-then rules (values of columns t
, s
):
rawdata_base['indicator'] = rawdata_base.apply(lambda row: '1' if row['t']=='2' , str(row['s']).isdigit() , int(row['s'])<15 else '0' if row['t']=='2' , str(row['s']).isdigit() , int(row['s'])>=15 else '1' if row['t']=='1' , str(row['s']).isdigit() , int(row['s'])<35 else '0' if row['t']=='1' , str(row['s']).isdigit() , int(row['s'])>=35 else '0' if 'a' in row['s'] else '0', axis 1)
i cannot figure out why error invalid syntax
pops @ line else '0', axis 1
you can't stack conditionals in python. ternary conditional operators can take 3 inputs (hence ternary): a if b else c
.
if want stack them, don't think want lambda
here. make own function:
def myfunc(row): if row['t']=='2' , str(row['s']).isdigit() , int(row['s'])<15: return '1' elif row['t']=='2' , str(row['s']).isdigit() , int(row['s'])>=15: return '0' ...
then in .apply
function, pass myfunc
Comments
Post a Comment