NealWalters
NealWalters

Reputation: 18197

Pandas: TypeError: unsupported operand type(s) for |: 'str' and 'str' with more complex selection

I was trying to build a new dataframe based on selection criteria from the first dataframe.

df_selected = df_in.loc[ (df_in['featureclass'] == 'P' | df_in['featureclass'] == 'A') &
                         (df_in['admin1code'] == 'TX')
                   ]

returns this error:

TypeError: unsupported operand type(s) for |: 'str' and 'str'

Upvotes: 0

Views: 782

Answers (1)

NealWalters
NealWalters

Reputation: 18197

Need to wrap each boolean in it's own set of parentheses.

df_selected = df_in.loc[ ((df_in['featureclass'] == 'P') | (df_in['featureclass'] == 'A')) &
                         (df_in['admin1code'] == 'TX')
                       ]

Upvotes: 1

Related Questions