Confusion about bitwise and and operator and

Hi, I am working on iner merge III session of panda. Link is :
https://www.codecademy.com/courses/data-processing-pandas/lessons/pandas-multiple-tables/exercises/inner-merge-iii

I am confused about bitwise and and normal “AND” . Can someone teach me some concept so this confusion is not with me anymore? This is the second time I encounter this confusion. Below is my code where correct answer use bitwise " & " operator:

import codecademylib3

import pandas as pd

sales = pd.read_csv('sales.csv')

print(sales)

targets = pd.read_csv('targets.csv')

print(targets)

men_women=pd.read_csv('men_women_sales.csv')

all_data=sales.merge(targets).merge(men_women)

print(all_data)

results=all_data[(all_data['revenue']>all_data['target'])AND(\

all_data['women']>all_data['men'])]

]

What’s different to the previous question?

If you’re uncomfortable with the operators both of these operations have function equivalents-
https://numpy.org/doc/stable/reference/generated/numpy.bitwise_and.html
https://numpy.org/doc/stable/reference/generated/numpy.logical_and.html

However, the logical and is designed as a binary operation. Unlike some other operators there is no dunder method to overload it’s behaviour so it cannot be used element-wise with the and keyword alone. You can see what it returns here https://docs.python.org/3/reference/expressions.html#boolean-operations.

The bitwise_and does have a relevant dunder method __and__ which means it can and is set-up to work element-wise.

import numpy as np


a = np.array([[1, 2,], [3, 4,]])
b = np.array([[5, 6,], [7, 8,]])

a and b
ValueError...

# element-wise operation
a & b
array([[1, 2],
       [3, 0]])

# Note that each element is the bitwise operation output...
1 & 5 == 1
2 & 6 == 2
3 & 7 == 3
4 & 8 == 0

all_data['revenue'] > all_data['target'] return a array of boolean value and should work with the logical "AND " key word to and all_data['women']>all_data['men'] but why it didn’t work?

The and keyword does not work in the way you expect with boolean arrays or arrays of any kind, you’ll either get a ValueError or it’ll try and use the truthiness of the array.

what’s truthiness of the array?

In short, the output of bool(obj) where in this case your object is an numpy ndarray.

If you have a web search you’ll find more info for Python’s truthiness but the evaluation of bool is basically it.