Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clipping negative values to 0 in a dataframe column (Pandas)

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!

like image 748
bbartling Avatar asked Jul 27 '17 19:07

bbartling


4 Answers

You could opt for clip_lower to do so in a single operation.

deltaT['data'] = (deltaT['hws'] - deltaT['hwr']).clip_lower(0)
like image 131
miradulo Avatar answered Sep 24 '22 08:09

miradulo


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
like image 28
cs95 Avatar answered Sep 22 '22 08:09

cs95


deltaT['data'] = (deltaT['hws'] - deltaT['hwr']).apply(lambda x: max(x, 0))
like image 42
Alexander Avatar answered Sep 24 '22 08:09

Alexander


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
like image 23
piRSquared Avatar answered Sep 22 '22 08:09

piRSquared