python - 从 Pandas 中的多个对象获取最大值.max()

标签 python pandas syntax-error max

我有一个这样的数据集:

ID   column_1  column_2 column_3 column 4
AAA   0.1        0.6      0.1     0.2
AAA   0.2        0.2      0.2     0.1
BBB   0.5        0.5      0.1     0.1
BBB   0.1        0.3      0.1     0.2
output = (df['column_1'] – df['column_2']) / (df['column_3'], 5 * df['column_1'], 0.01 * abs(df['column_4'])).max(axis=1)

但这给了我一个错误:
SyntaxError: invalid character in identifier

预期输出可描述为以下示例:
对于第一行-输出=(0.1-0.6)/max(0.1,5 * 0.1,0.01 * abs(0.2))= -0.5/0.5 = -1
有人可以给我一些提示吗?提前致谢。

最佳答案

解决方案是通过 concat 加入它们,然后每行使用max:

s = pd.concat([df['column_3'], 
               5 * df['column_1'], 
               0.01 * abs(df['column_4'])], axis=1).max(axis=1)
output = (df['column_1'] - df['column_2']) / s
print (output)
0   -1.0
1    0.0
2    0.0
3   -0.4
dtype: float64

关于python - 从 Pandas 中的多个对象获取最大值.max(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60490254/

相关文章:

sql - 在单引号中使用 'like'时的Postgres语法错误

ruby-on-rails - C :/Sites/rails_projects/sample_app/app/controllers/users_controller.rb:23: syntax error, unexpected $end, expecting kEND

php - 未捕获的语法错误意外的字符串

带有多个argv的python命令行

python - str.format() 选项不起作用

java - 按顺序向 Kafka 发送不同消息类型的良好做法

python - 我想计算 Pandas DataFrame 中每一列的具体数字?

python - Numpy 操作对 groupby 无效。使用 .groupby(...).mean() 代替,这就是我所做的

python - 当一行包含另一行的字符串时如何匹配行?

python - PyQt 工具栏默认位于第二行