Pandas: Element-wise logical NOT and logical OR operators

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
6 min

banner

# Table of Contents

  1. Pandas: Element-wise logical NOT operator
  2. If your Series has missing values, convert its values to boolean
  3. Pandas: Element-wise logical NOT operator using numpy.invert
  4. Pandas: Element-wise logical OR operator
  5. Pandas: Element-wise logical OR operator with numpy.logical_or
  6. Pandas: Element-wise logical OR operator with numpy.logical_or.reduce

Note: if you need to perform element-wise logical OR, click on the following subheading:

# Pandas: Element-wise logical NOT operator

The tilde operator ~ can be used to perform a logical NOT operation in Pandas.

The tilde ~ operator will invert the boolean values in the Series.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], 'is_subscribed': pd.Series([True, False, False, True]) }) print(df) print('-' * 50) inverted = ~df['is_subscribed'] print(inverted)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name salary is_subscribed 0 Alice 175.1 True 1 Bobby 180.2 False 2 Carl 190.3 False 3 Dan 205.4 True -------------------------------------------------- 0 False 1 True 2 True 3 False Name: is_subscribed, dtype: bool

using logical not operator in pandas

As shown in the code sample, the tilde ~ operator inverts the values in the Series and serves as a logical NOT operator.

Here is an example that uses the tilde ~ operator directly on a Series.

main.py
import pandas as pd s = pd.Series([True, False, False, True]) inverted = ~s # 0 False # 1 True # 2 True # 3 False # dtype: bool print(inverted)

using tilde operator directly on series

The code for this article is available on GitHub

You might also see the - operator being used.

Using the - (minus) operator is equivalent to using ~ (tilde).

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], 'is_subscribed': pd.Series([True, False, False, True]) }) print(df) print('-' * 50) inverted = -df['is_subscribed'] print(inverted)

Running the code sample produces the following output.

shell
name salary is_subscribed 0 Alice 175.1 True 1 Bobby 180.2 False 2 Carl 190.3 False 3 Dan 205.4 True -------------------------------------------------- 0 False 1 True 2 True 3 False Name: is_subscribed, dtype: bool

using logical not with minus operator

Note: if you need to perform element-wise logical OR, click on the following subheading:

# If your Series has missing values, convert its values to boolean

If your Series has missing values, use the DataFrame.astype() method to convert its values to booleans before using the logical NOT ~ operator.

main.py
import pandas as pd import numpy as np df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], 'is_subscribed': pd.Series([True, False, np.nan, True]) }) inverted = ~df['is_subscribed'].astype(bool) # 0 False # 1 True # 2 False # 3 False # Name: is_subscribed, dtype: bool print(inverted)

convert to booleans before using logical not

The code for this article is available on GitHub

The is_subscribed Series contains NaN values, so we used the astype() method to convert the values in the Series to booleans before using the logical NOT operator.

If you skip the conversion step, you will likely get an error or incorrect results.

Note: if you need to perform element-wise logical OR, click on the following subheading:

# Pandas: Element-wise logical NOT operator using numpy.invert

You can also use the numpy.invert method as a logical NOT operator in Pandas.

First, make sure you have the numpy module installed.

shell
pip install numpy # or with pip3 pip3 install numpy

Now, import the module and use the numpy.invert() method.

main.py
import numpy as np import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'salary': [175.1, 180.2, 190.3, 205.4], 'is_subscribed': pd.Series([True, False, False, True]) }) print(df) print('-' * 50) inverted = np.invert(df['is_subscribed']) print(inverted)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name salary is_subscribed 0 Alice 175.1 True 1 Bobby 180.2 False 2 Carl 190.3 False 3 Dan 205.4 True -------------------------------------------------- 0 False 1 True 2 True 3 False Name: is_subscribed, dtype: bool

pandas element wise logical not using numpy invert

The numpy.invert method computes bit-wise NOT, element-wise.

However, note that using numpy.invert() is a bit less performant than using the tilde ~ operator and the minus - operator.

# Pandas: Element-wise logical OR operator

Use the pipe | operator to perform logical OR in Pandas.

When the operator is used, at least one of the specified conditions has to be met for the row to be included in the result.

The example selects the rows where the "experience" column value is greater than 5 or the "salary" value is greater than 190.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [1, 3, 5, 7], 'salary': [175.1, 180.2, 190.3, 205.4], }) print(df) print('-' * 50) result = df[(df.experience > 5) | (df.salary > 190)] print(result)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name experience salary 0 Alice 1 175.1 1 Bobby 3 180.2 2 Carl 5 190.3 3 Dan 7 205.4 -------------------------------------------------- name experience salary 2 Carl 5 190.3 3 Dan 7 205.4

element wise logical or operator

Notice that we wrapped each condition in parentheses and separated them using the logical OR (|) operator.

main.py
result = df[(df.experience > 5) | (df.salary > 190)]

The example selects the rows for which the "experience" value is greater than 5 or the "salary" value is greater than 190.

You can use the logical OR | operator to check for as many conditions as necessary, just make sure that:

  1. Each condition is wrapped in parentheses ().
  2. The conditions are separated by the logical OR | operator.

The same approach can be used when checking for string values in conditions.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [1, 3, 5, 7], 'salary': [175.1, 180.2, 190.3, 205.4], }) print(df) print('-' * 50) result = df[(df.name == 'Alice') | (df.name == 'Bobby')] print(result)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name experience salary 0 Alice 1 175.1 1 Bobby 3 180.2 2 Carl 5 190.3 3 Dan 7 205.4 -------------------------------------------------- name experience salary 0 Alice 1 175.1 1 Bobby 3 180.2

element wise logical or operator in pandas with string values

The example selects the rows where the "name" column value is equal to "Alice" or "Bobby".

# Pandas: Element-wise logical OR operator with numpy.logical_or

You can also use the numpy.logical_or method as a logical OR operator in Pandas.

First, make sure you have the numpy module installed.

shell
pip install numpy # or with pip3 pip3 install numpy

Now, import the module and use the numpy.logical_or() method.

main.py
import numpy as np import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [1, 3, 5, 7], 'salary': [175.1, 180.2, 190.3, 205.4], }) print(df) print('-' * 50) result = df[np.logical_or(df.experience > 5, df.salary > 190)] print(result)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
0 Alice 1 175.1 1 Bobby 3 180.2 2 Carl 5 190.3 3 Dan 7 205.4 -------------------------------------------------- name experience salary 2 Carl 5 190.3 3 Dan 7 205.4

pandas element wise logical or with numpy logical or

The numpy.logical_or method returns a boolean result.

The array contains True values if at least one of the supplied conditions is met, otherwise, False is returned.

main.py
import numpy as np import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [1, 3, 5, 7], 'salary': [175.1, 180.2, 190.3, 205.4], }) # 0 False # 1 False # 2 True # 3 True # dtype: bool print(np.logical_or(df.experience > 5, df.salary > 190))

# Pandas: Element-wise logical OR operator with numpy.logical_or.reduce

If you need to specify more than two conditions when calling numpy.logical_or(), use the numpy.logical_or.reduce() method.

main.py
import numpy as np import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [1, 3, 5, 7], 'salary': [175.1, 180.2, 190.3, 205.4], }) print(df) print('-' * 50) result = df[np.logical_or.reduce([df.experience > 5, df.salary > 190, df.name == 'Alice'])] print(result)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name experience salary 0 Alice 1 175.1 1 Bobby 3 180.2 2 Carl 5 190.3 3 Dan 7 205.4 -------------------------------------------------- name experience salary 0 Alice 1 175.1 2 Carl 5 190.3 3 Dan 7 205.4

using numpy logical or reduce method in pandas

One of the following 3 conditions has to be met for the row to get included in the resulting DataFrame:

  1. The "experience" value has to be greater than 5.
  2. The "salary" value has to be greater than 190.
  3. The "name" value has to be equal to the string "Alice".

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.