Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I have a pandas Series object containing boolean values. How can I get a series containing the logical NOT of each value?

For example, consider a series containing:

True

True

True

False

The series I'd like to get would contain:

False

False

False

True

This seems like it should be reasonably simple, but apparently I've misplaced my mojo =(

1 Answer

0 votes
by (41.4k points)

For inverting a boolean Series, use ~s:

In [7]: s = pd.Series([True, True, False, True])

In [8]: ~s

Out[8]: 

0    False

1    False

2     True

3    False

dtype: bool

Related questions

0 votes
1 answer
asked Jul 31, 2019 in Data Science by sourav (17.6k points)
0 votes
2 answers
0 votes
1 answer

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...