Python np.where:“;语法错误:关键字can';不要成为一种表达;
我试图结合以下逻辑 在A列中,其中B列=堆栈,然后A列*100,否则保持A列不变Python np.where:“;语法错误:关键字can';不要成为一种表达;,python,Python,我试图结合以下逻辑 在A列中,其中B列=堆栈,然后A列*100,否则保持A列不变 df['Value'] = np.where(df['columnB'] = 'Stack', df['Value'] * 100) 为什么我会得到一个SyntaxError:keyword不能是一个表达式?您必须为二进制条件提供一个条件和两个结果 在不太了解您的情况下,dataframe我认为您应该做以下事情: df['Value'] = np.where(df['columnB'] == 'Stack', d
df['Value'] = np.where(df['columnB'] = 'Stack', df['Value'] * 100)
为什么我会得到一个
SyntaxError:keyword不能是一个表达式?您必须为二进制条件提供一个条件和两个结果
在不太了解您的情况下,dataframe
我认为您应该做以下事情:
df['Value'] = np.where(df['columnB'] == 'Stack', df['columnA']*100, df['Value'])
这是因为在it国家:
numpy.where(condition[, x, y])
Return elements chosen from x or y depending on condition.
Parameters:
condition : array_like, bool
Where True, yield x, otherwise yield y.
因此,如果columnB
是'Stack'
,则df['Value']
将填充columnA
乘以100,否则,它将保留存储在Value
的值。我看到一个错误df['columnB']='Stack'
应该是df['columnB']='Stack'
该死,我现在看到了。我的错误。。那么我应该删除这个吗?这取决于你是否认为其他人会遇到这个问题,并且可能会觉得这个有用。但这是相当琐碎的。