python - np.where : "SyntaxError: keyword can' t be an expression"

标签 python

我正在尝试合并以下逻辑

在 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/

相关文章:

python - 如何在 Python 中拆分和解析字符串?

python - 不使用 django 表单模板从 HTML 获取数据

python - 使用 lex/yacc 标记遗留数据

python - 提高魔杖转换的质量

python - 如何使用python计算SQL数据库中的记录数

Python MySQL 返回错误的查询行,但 Phpmyadmin 显示正确的行

python - 像第一次安装一样还原 Anaconda

python - 在 Python 中管理 Selenium 的多个实例

python - 我无法让 'player' 在 pygame 中双向移动,我该怎么做?

jquery - Flask 无法加载 css 文件