Search code examples
pythonpandasboolean-indexing

Star (*) within Pandas boolean indexing


Because of a typo, I happened upon some Pandas DataFrame boolean indexing syntax that I'm not familiar with and I can't find any information describing what is actually happening.

I was trying to retrieve a dataframe based on two conditions with an & but typed a * instead and I was surprised to see that the results are the same

    ex1 = dist[(dist['token'].str.isalnum()) * (dist['count']>2000)]
    ex2 = dist[(dist['token'].str.isalnum()) & (dist['count']>2000)]

    ex1 == ex2
    # returns
    #     token  count
    #     True   True
    #     True   True
    #     True   True
    #     True   True
    #     True   True
    #     True   True
    #     True   True
    #     True   True
    #     True   True
    #     True   True

Solution

  • & is a bitwise logical operator, whereas * will cast booleans as real

    You can get more details here https://www.pyblog.in/programming/bitwise-operators-in-python/