嗨,我有以下代码:
def label_exception (row):
if row['Dividend with no set frequency'] >= 1 :
return 'ExceptionCase'
if (row['count'] != 12) & (row['Monthly dividend'] > 1) :
return 'ExceptionCase '
if (row['count'] != 4) & (row['Quarterly dividend'] > 1) :
return 'ExceptionCase'
if (row['count'] != 2) & (row['Semi-annual dividend'] > 1):
return 'ExceptionCase'
if (row['count'] != 1) & (row['Annual dividend'] > 1):
return 'ExceptionCase'
if row['Semi-annual dividend'] is not in :
else:
return 'NotanExceptionCase'
Final_Output['exception'] = Final_Output.apply (lambda row: label_exception(row), axis=1)
现在,如果数据帧中不存在上述任何一行值,则其给出的关键帧错误我想做的是,如果值不存在,它应该忽略并继续到下一个条件,而不是给出一个关键错误
如何落实
您应该我们尝试除外,即:
try:
if row['something'] > something:
return 'something'
except KeyError:
pass
您还应该检查如何引发异常,而不是简单地将它们打印为字符串。
您应该研究python中的try-and-except和错误捕获。
https://docs.python.org/3/tutorial/errors.html