Search code examples
pythonpandasdataframecriteria

Bracketing when filtering on mutiple fields


A question on brackets when filtering:

df.info()

RangeIndex: 4005 entries, 0 to 4004
Data columns (total 41 columns):
currency_str                   4005 non-null object
state                          4005 non-null object

display(
        df[
            df['currency_str'] == 'INR'  
        ])

Displays my INR rows correctly.

When I add the second filter I require double brackets around both otherwise I get errors.

display(

        df[
            (df['currency_str'] == 'INR')    &  
            (df['state'].str.contains('Done'))
        ])

What's going on under the hood here with Pandas? The & is not enough? Is this specifically related to string fields where the criteria is enclosed in ''?

No Brackets

display(
        df[
            df['currency_str'] == 'INR'    &  
            df['state'].str.contains('Done')
        ])
TypeError: cannot compare a dtyped [bool] array with a scalar of type [bool]

One set of brackets

display(
        df[
            (df['currency_str'] == 'INR'    &  
            df['state'].str.contains('Done'))
        ])
TypeError: cannot compare a dtyped [bool] array with a scalar of type [bool]

Solution

  • You should adding () for each condition

    df=pd.DataFrame({'currency_str':['INR','INR','None'],'state':['LOLDone','l','Done']})
    
    df[(df['currency_str'] == 'INR') &(df['state'].str.contains('Done'))]
    Out[789]: 
      currency_str    state
    0          INR  LOLDone