How to get inverse boolean value in pandas.DataFrame

How can I reverse True/False value in pandas?

pandas.DataFrame is useful for handling table data.

With using it, we can handle CSV or Excel data as table data.

Sometimes table data has boolean data like True/False.

In order to reverse boolean values, what should we do ?

So today I will introduce about "How to get inverse boolean value in pandas.DataFrame".

目次

Prepare data

So how can we get inverse boolean values in DataFrame ?

Before trying to reverse boolean values, we prepare DataFrame that contains boolean column.

Data

import pandas as pd

data_list1 = [
    ["a",True],
    ["b",True],
    ["c",False],
    ["d",True]
]
col_list1 = ["col1","col2"]
df1 = pd.DataFrame(data=data_list1, columns=col_list1)
print(df1)

#   col1   col2
# 0    a   True
# 1    b   True
# 2    c  False
# 3    d   True

Now we have sample data.

With using this data, we will try to reverse boolean values.

"NOT" operator can't reverse DataFrame boolean values

About "reverse", we will imagine NOT operator.

But NOT operator can't reverse DataFrame boolean values.

You can check like below.

df1["not_col2"] = not df1["col2"]

print(df1)

# ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Like this, if we use NOT operator with pandas.DataFrame, it causes error.

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

According to the message, you can try to use .bool(). But you will face same error.

print(df1["col2"].bool())

# ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

NOT operator is object-wise. So it couldn't do element-wise logical operation.

ust be a boolean scalar value, either True or False. It will raise a ValueError if the Series or DataFrame does not have exactly 1 element, or that element is not boolean (integer values 0 and 1 will also raise an exception).

pandas.DataFrame.bool — pandas 1.4.3 documentation

If it has 1 element like Series, we can reverse it by NOT operator.

df1_false = df1.loc[
  df1["col2"] ==  False
]
print(df1_false)
#   col1   col2
# 2    c  False

print(df1_false["col2"].bool())
# False

print(not df1_false["col2"].bool())
# True

But it doesn't mean that we could reverse boolean value in pandas.DataFrame.

How can we reverse it ?

How to get inverse boolean value in pandas.DataFrame

not operator can't reverse DataFrame boolean values.

So how can we get inverse boolean value in pandas.DataFrame ?

In order to get inverse boolean DataFrame, we can use ~ operator.

With using ~ operator, we can reverse boolean value in pandas.DataFrame.

df1["not_col2"] = ~df1["col2"]

print(df1)

#   col1   col2  not_col2
# 0    a   True     False
# 1    b   True     False
# 2    c  False      True
# 3    d   True     False

Like this sample code, we could reverse boolean values in col2 column.

Conclusion

Today I explained about "How to get inverse boolean value in pandas.DataFrame".

Important points are below.

  • In order to reverse DataFrame boolean column, use ~.
  • If you use not with DataFrame, it causes error.

Once we understand it, it is easy to use.

この記事が気に入ったら
いいね または フォローしてね!

If you like this article, please share !
  • URLをコピーしました!
  • URLをコピーしました!

Author

karasanのアバター karasan System engineer

Mid-career engineer (AI, Data science, Salesforce, etc.).
Good at Python and SQL.

目次