Last updated: Apr 12, 2024
Reading time·6 min
Note: if you need to perform element-wise logical OR, click on the following subheading:
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
.
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.
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
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
.
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)
You might also see the -
operator being used.
Using the -
(minus) operator is equivalent to using ~
(tilde).
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.
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
Note: if you need to perform element-wise logical OR, click on the following subheading:
If your Series
has missing values, use the
DataFrame.astype()
method to convert its values to booleans before using the logical NOT ~
operator.
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)
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:
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.
pip install numpy # or with pip3 pip3 install numpy
Now, import the module and use the numpy.invert()
method.
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)
Running the code sample produces the following output.
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
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.
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
.
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)
Running the code sample produces the following output.
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
Notice that we wrapped each condition in parentheses and separated them using the logical OR (|) operator.
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:
()
.|
operator.The same approach can be used when checking for string values in conditions.
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)
Running the code sample produces the following output.
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
The example selects the rows where the "name"
column value is equal to
"Alice"
or "Bobby"
.
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.
pip install numpy # or with pip3 pip3 install numpy
Now, import the module and use the numpy.logical_or()
method.
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)
Running the code sample produces the following output.
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
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.
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))
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.
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)
Running the code sample produces the following output.
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
One of the following 3 conditions has to be met for the row to get included in
the resulting DataFrame
:
"experience"
value has to be greater than 5
."salary"
value has to be greater than 190
."name"
value has to be equal to the string "Alice"
.You can learn more about the related topics by checking out the following tutorials: