1

I have this dataframe

dd = pd.DataFrame({'a':[1,5,3],'b':[3,2,3],'c':[2,4,5]})

   a  b  c
0  1  3  2
1  5  2  4
2  3  3  5

I just want to replace numbers of column a and b which are smaller than column c numbers. I want to this operation row wise

I did this

dd.applymap(lambda x: 0 if x < x['c'] else x )

I get error

TypeError: 'int' object is not subscriptable

I understood x is a int but how to get value of column c for that row

I want this output

   a  b  c
0  0  3  2
1  5  0  4
2  0  0  5

1 Answer 1

2

Use DataFrame.mask with DataFrame.lt:

df = dd.mask(dd.lt(dd['c'], axis=0), 0)
print (df)
   a  b  c
0  0  3  2
1  5  0  4
2  0  0  5

Or you can set values by compare broadcasting by column c:

dd[dd < dd['c'].to_numpy()[:, None]] = 0
print (dd)
   a  b  c
0  0  3  2
1  5  0  4
2  0  0  5
Sign up to request clarification or add additional context in comments.

Comments

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.