为pandas DataFrame列返回最大值零或值 [英] Return max of zero or value for a pandas DataFrame column

查看:203
本文介绍了为pandas DataFrame列返回最大值零或值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将pandas DataFrame列中的负值替换为零.

I want to replace negative values in a pandas DataFrame column with zero.

是否有更简洁的方法来构造该表达式?

Is there a more concise way to construct this expression?

df['value'][df['value'] < 0] = 0

推荐答案

这是规范的实现方式,虽然不一定更简洁,但更灵活(因为您可以将其应用于任意列)

Here is the canonical way of doing it, while not necessarily more concise, is more flexible (in that you can apply this to arbitrary columns)

In [39]: df = DataFrame(randn(5,1),columns=['value'])

In [40]: df
Out[40]: 
      value
0  0.092232
1 -0.472784
2 -1.857964
3 -0.014385
4  0.301531

In [41]: df.loc[df['value']<0,'value'] = 0

In [42]: df
Out[42]: 
      value
0  0.092232
1  0.000000
2  0.000000
3  0.000000
4  0.301531

这篇关于为pandas DataFrame列返回最大值零或值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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