我正在尝试合并以下逻辑
在 A 列中,其中 B 列 = Stack 然后是 A 列 * 100,否则保持 A 列不变。
df['Value'] = np.where(df['columnB'] = 'Stack', df['Value'] * 100)
为什么我在这里得到一个SyntaxError: keyword can't be an expression
?
最佳答案
您必须为二元条件提供一个条件和两个结果
在不太了解你的情况下 dataframe
我相信你应该做如下的事情:
df['Value'] = np.where(df['columnB'] == 'Stack', df['columnA']*100, df['Value'])
这是因为在 documentation它指出:
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
关于python - np.where : "SyntaxError: keyword can' t be an expression",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58665507/