Python

How to reverse boolean value in pandas.DataFrame

Share this for your friends.

How to reverse 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 reverse boolean value in pandas.DataFrame".

Author


Mid-carieer engineer (AI, system). Good at Python and SQL.

Advantage to read

You can understand about How to reverse boolean value in pandas.DataFrame. Then you don't have to concern about programming with pandas.DataFrame.


Prepare data

So how can we reverse boolean values in DataFrame ?

Before trying to reverse boolean values, 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.

SAMPLE

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, if we use .bool(), same error occurs.

SAMPLE

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.

This must 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).

Reference: pandas.DataFrame.bool — pandas 1.4.3 documentation


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

SAMPLE

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 reverse boolean value in pandas.DataFrame

not operator can't reverse DataFrame boolean values.

So How can we reverse boolean value in pandas.DataFrame ?


The solution is ~.

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

SAMPLE

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 reverse boolean value in pandas.DataFrame".

Important points are below.

Point

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


Once we learn it, it is easy to use.


There are some other articles about pandas.Dataframe or NumPy.ndarray.

If you interested in them, please read them.


Share this for your friends.

If you felt this article is useful, please share.

にほんブログ村 IT技術ブログへ

-Python
-

© 2023 ITips