明智地获得[0,x]元素的最小值 [英] Get the min of [0, x] element wise for a column

查看:56
本文介绍了明智地获得[0,x]元素的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算一列,其值是矢量化操作相对于其他列的结果:

I need to compute a column where the value is the result of a vectorized operation over other columns:

df["new_col"] = df["col1"] - min(0,df["col2"])

但是,事实证明,我不能像上面的语法那样使用min.那么,使最小值在零和给定的pandas列之间的正确方法是什么?

It turned out, however, that I cannot use min as in the above syntax. So, what is the right way to get the min between zero and a given value of pandas column?

推荐答案

,您可以使用 numpy.minimum 来查找数组中元素的最小值

you can use numpy.minimum to find the element-wise minimum of an array

import numpy as np
df["new_col"] = df["col1"] - np.minimum(0,df["col2"])

这篇关于明智地获得[0,x]元素的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆