将数据框列中的负值剪切为 0 (Pandas)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45359733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clipping negative values to 0 in a dataframe column (Pandas)
提问by HenryHub
I am doing a simple math equation of pandas series data frames, and some of the values are going negative when compiling a lot of the data. Is there code that I can add to ensure values of the subtraction math only go to minimum of zero? This is what I have so far:
我正在做一个 Pandas 系列数据框的简单数学方程,在编译大量数据时,有些值会变成负数。有没有我可以添加的代码来确保减法数学的值只达到零的最小值?这是我到目前为止:
deltaT['data'] = (deltaT['hws'] - deltaT['hwr'])
Thanks!
谢谢!
回答by miradulo
You could opt for clip_lower
to do so in a single operation.
您可以选择clip_lower
在单个操作中执行此操作。
deltaT['data'] = (deltaT['hws'] - deltaT['hwr']).clip_lower(0)
回答by cs95
You can create deltaT['data']
and then use df.loc
to set the negative values to 0.
您可以创建deltaT['data']
然后使用df.loc
将负值设置为 0。
deltaT['data'] = (deltaT['hws'] - deltaT['hwr'])
deltaT.loc[deltaT['data'] < 0, 'data'] = 0
回答by piRSquared
Option 1
simple
选项 1
简单
deltaT['data'] = deltaT.eval('(hws - hwr) * (hws > hwr)')
Consider deltaT
考虑 deltaT
deltaT = pd.DataFrame(dict(hws=[5, 8], hwr=[8, 5]))
deltaT.assign(data=deltaT.eval('(hws - hwr) * (hws > hwr)'))
hwr hws data
0 8 5 0
1 5 8 3
Option 2
Same as option 1, but using numpy arrays
选项 2
与选项 1 相同,但使用 numpy 数组
r, s = (deltaT[c].values for c in ['hwr', 'hws'])
deltaT.assign(data=(s - r) * (s > r))
hwr hws data
0 8 5 0
1 5 8 3
Option 3
creative attempt
选项 3
创意尝试
deltaT.assign(data=deltaT.eval('hws - hwr').to_frame().assign(_=0).max(1))
hwr hws data
0 8 5 0
1 5 8 3
回答by Alexander
deltaT['data'] = (deltaT['hws'] - deltaT['hwr']).apply(lambda x: max(x, 0))