How to Create Boolean Array From Dataframe in Python

To create boolean array from dataframe in python, you can use boolean indexing.

Method: Use Boolean Indexing

(df['column_name'] < n).values

The following example shows how to create boolean array from dataframe using boolean indexing in python.

Using Boolean Indexing

Let’s see how to use boolean indexing to create boolean array:

# Import pandas library
import pandas as pd

# Declare dictionary
df = pd.DataFrame({
    'Time': ['01-03-2023', '01-03-2023', '01-03-2023', '01-03-2023', '02-03-2023', '02-03-2023'],
    'Product_Code': ['A-101', 'A-102', 'A-103', 'B-101', 'B-102', 'B-104'],
    'Product_Name': ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'],
    'Price': [4500, 550, 250, 50, 350, 50],
    'Status': [1, 1, 1, 0, 1, 1]})

# Create boolean array
bool_arr = (df['Price'] < 300).values

# Show array
print(bool_arr)

Output:

[False False  True  True False  True]

The output shows boolean array which created from dataframe.