I have two pandas dataframes, one containing data and another consisting of True/False values
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 |
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?
>Solution :
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