[英]Invalid syntax error when applying lambda for multiple columns processing
我有以下代碼應根據if-then規則( T
和S
列的值)將0或1放入INDICATOR
列:
rawdata_base['INDICATOR'] = rawdata_base.apply(lambda row:
'1' if row['T']=='2' and str(row['S']).isdigit() and int(row['S'])<15
else '0' if row['T']=='2' and str(row['S']).isdigit() and int(row['S'])>=15
else '1' if row['T']=='1' and str(row['S']).isdigit() and int(row['S'])<35
else '0' if row['T']=='1' and str(row['S']).isdigit() and int(row['S'])>=35
else '0' if 'A' in row['S']
else '0', axis 1)
我無法弄清楚為什么錯誤invalid syntax
在else '0', axis 1
彈出
您不能在python中堆疊類似的條件。 三元條件運算符只能接受3個輸入(因此為三元): a if b else c
。
如果要堆疊它們,那么我認為您不需要在這里的lambda
。 發揮自己的作用:
def myfunc(row):
if row['T']=='2' and str(row['S']).isdigit() and int(row['S'])<15:
return '1'
elif row['T']=='2' and str(row['S']).isdigit() and int(row['S'])>=15:
return '0'
...
然后在您的.apply
函數中,傳遞myfunc
您缺少“ =”。 應該是'axis = 1'而不是'axis 1'
正如@gipsy所提到的,錯誤之一是缺少=
( axis=1
)。 但是,主要錯誤是elif 'A' in str(row['S']):
的代碼elif 'A' in str(row['S']):
這行代碼中缺少str
elif 'A' in str(row['S']):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.