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:
deltaT['data'] = (deltaT['hws'] - deltaT['hwr'])
Thanks!
You could opt for clip_lower
to do so in a single operation.
deltaT['data'] = (deltaT['hws'] - deltaT['hwr']).clip_lower(0)
You can create deltaT['data']
and then use df.loc
to set the negative values to 0.
deltaT['data'] = (deltaT['hws'] - deltaT['hwr'])
deltaT.loc[deltaT['data'] < 0, 'data'] = 0
deltaT['data'] = (deltaT['hws'] - deltaT['hwr']).apply(lambda x: max(x, 0))
Option 1
simple
deltaT['data'] = deltaT.eval('(hws - hwr) * (hws > hwr)')
Consider 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
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
deltaT.assign(data=deltaT.eval('hws - hwr').to_frame().assign(_=0).max(1))
hwr hws data
0 8 5 0
1 5 8 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With