首页 > 解决方案 > 如果我的数据框中的另一列中有一个空值,我正在尝试用一个值更新一个新列

问题描述

如果列为空,我正在尝试Report['Failure Reason']用值更新列。报告是我的数据框。"Interface Failure"Report['BSD']

我尝试使用下面的代码,它抛出

“SyntaxError:关键字不能是表达式”错误

Report['Failure Reason'] = np.where(Report['BSD'] = '', 'Interface Failure', ' ')

如果Report['BSD']为空,Report['Failure Reason']则应使用 value 更新列"Interface Failure",否则应忽略

标签: python-3.xpandasdataframe

解决方案


如果有空字符串,==仅使用:

Report['Failure Reason'] = np.where(Report['BSD'] == '', 'Interface Failure', ' ')

或者,如果有缺失值,请通过以下方式测试Series.isna

Report['Failure Reason'] = np.where(Report['BSD'].isna(), 'Interface Failure', ' ')

推荐阅读