Return max of zero or value for a pandas DataFrame column

Question:

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
Asked By: bjornarneson

||

Answers:

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
Answered By: Jeff

You could use the clip method:

import pandas as pd
import numpy as np
df = pd.DataFrame({'value': np.arange(-5,5)})
df['value'] = df['value'].clip(0, None)
print(df)

yields

   value
0      0
1      0
2      0
3      0
4      0
5      0
6      1
7      2
8      3
9      4
Answered By: unutbu

Another possibility is numpy.maximum(). This is more straight-forward to read in my opinion.

import pandas as pd
import numpy as np
df['value'] = np.maximum(df.value, 0)

It’s also significantly faster than all other methods.

df_orig = pd.DataFrame({'value': np.arange(-1000000, 1000000)})

df = df_orig.copy()
%timeit df['value'] = np.maximum(df.value, 0)
# 100 loops, best of 3: 8.36 ms per loop

df = df_orig.copy()
%timeit df['value'] = np.where(df.value < 0, 0, df.value)
# 100 loops, best of 3: 10.1 ms per loop

df = df_orig.copy()
%timeit df['value'] = df.value.clip(0, None)
# 100 loops, best of 3: 14.1 ms per loop

df = df_orig.copy()
%timeit df['value'] = df.value.clip_lower(0)
# 100 loops, best of 3: 14.2 ms per loop

df = df_orig.copy()
%timeit df.loc[df.value < 0, 'value'] = 0
# 10 loops, best of 3: 62.7 ms per loop

(notebook)

Answered By: Dorian B.

Let’s take only values greater than zero, leaving those which are negative as NaN (works with frames not with series), then impute.

df[df > 0].fillna(0)
Answered By: Coolkau

For completeness, np.where is also a possibility, which is faster than most answers here. The np.maximum answer is the best approach though, as it’s faster and more concise than this.

df['value'] = np.where(df.value < 0, 0, df.value)
Answered By: Max Ghenis

Or where to check:

>>> import pandas as pd,numpy as np
>>> df = pd.DataFrame(np.random.randn(5,1),columns=['value'])
>>> df
      value
0  1.193313
1 -1.011003
2 -0.399778
3 -0.736607
4 -0.629540
>>> df['value']=df['value'].where(df['value']>0,0)
>>> df
      value
0  1.193313
1  0.000000
2  0.000000
3  0.000000
4  0.000000
>>> 
Answered By: U12-Forward
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.