xxanissrxx
xxanissrxx

Reputation: 129

How to replace elements if two booleans are true in a Pandas dataframe

I am trying to accomplish the following:

Y_Pred[Y_Pred < 0.99 and Y_Pred > 0.98] = 1
Y_Pred[Y_Pred <=0.98 or Y_Pred >= 0.99] = 0

But the and and the or aren't correct?

Upvotes: 0

Views: 30

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You should use bitwise operations | and & instead of and and or. Also, you can use np.where like this:

Y_Pred = np.where((Y_Pred < 0.99) & (Y_Pred > 0.98),1,0)

or just:

Y_Pred = ((Y_Pred < 0.99) & (Y_Pred > 0.98)).astype(int)

Upvotes: 4

Related Questions