bbartling
bbartling

Reputation: 3504

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!

Upvotes: 10

Views: 11216

Answers (4)

piRSquared
piRSquared

Reputation: 294488

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

Upvotes: 3

Alexander
Alexander

Reputation: 109626

deltaT['data'] = (deltaT['hws'] - deltaT['hwr']).apply(lambda x: max(x, 0))

Upvotes: 4

miradulo
miradulo

Reputation: 29710

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

deltaT['data'] = (deltaT['hws'] - deltaT['hwr']).clip_lower(0)

Upvotes: 11

cs95
cs95

Reputation: 402813

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

Upvotes: 5

Related Questions