3

I have a pandas dataframe as follows.

   a  b  c  d  e
a  0  1  0  1  1
b  1  0  1  6  3
c  0  1  0  1  2
d  5  1  1  0  8
e  1  3  2  8  0

I want to replace values that is below 6 <=5 with 0. So my output should be as follows.

   a  b  c  d  e
a  0  0  0  0  0
b  0  0  0  6  0
c  0  0  0  0  0
d  0  0  0  0  8
e  0  0  0  8  0

I was trying to do this using the following code.

df['a'].replace([1, 2, 3, 4, 5], 0)
df['b'].replace([1, 2, 3, 4, 5], 0)
df['c'].replace([1, 2, 3, 4, 5], 0)
df['d'].replace([1, 2, 3, 4, 5], 0)
df['e'].replace([1, 2, 3, 4, 5], 0)

However, I am sure that there is a more easy way of doing this task in pandas.

I am happy to provide more details if needed.

2 Answers 2

5

Using mask

df=df.mask(df<=5,0)
df
Out[380]: 
   a  b  c  d  e
a  0  0  0  0  0
b  0  0  0  6  0
c  0  0  0  0  0
d  0  0  0  0  8
e  0  0  0  8  0
Sign up to request clarification or add additional context in comments.

Comments

3

For performance, I recommend np.where. You can assign the array back inplace using sliced assignment (df[:] = ...).

df[:] = np.where(df < 6, 0, df)
df

   a  b  c  d  e
a  0  0  0  0  0
b  0  0  0  6  0
c  0  0  0  0  0
d  0  0  0  0  8
e  0  0  0  8  0

Another option involves fillna:

df[df>=6].fillna(0, downcast='infer')

   a  b  c  d  e
a  0  0  0  0  0
b  0  0  0  6  0
c  0  0  0  0  0
d  0  0  0  0  8
e  0  0  0  8  0

2 Comments

Didn't know we can use np where on the entire df. Very nice :)
@anky_91 Happy my post taught you something new!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.