0

I have two pandas dataframes, one containing data and another consisting of True/False values

  1. iris
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
  1. iris_bool
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
True False True False False
False True False False False
False False False False True

I would like to replace values in table 1 with NA/NaNs based on the corresponding boolean in table 2. Is there an easy way to do this?

2 Answers 2

1

Try mask:

iris = iris.mask(~iris_bool)

Output:

>>> iris
   Sepal.Length  Sepal.Width  Petal.Length  Petal.Width Species
0           5.1          NaN           1.4          NaN     NaN
1           NaN          3.0           NaN          NaN     NaN
2           NaN          NaN           NaN          NaN  setosa
Sign up to request clarification or add additional context in comments.

2 Comments

It works! Thank you! Do you mind if I ask what the ~ is doing in this expression?
It's inverting it, so every True becomes False and every False becomes True.
0

You can just assign

iris[~iris_bool]=np.nan

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.