7

I would like to slice a pandas.DataFrame which satisfies condition A or condition B. Most of the search results only show how to slice dataframe using "and". So I wonder if it is possible to use "or" operator without converting (A and B) to (not (not A and not B))? Because sometimes there are many "or" conditions needed, and converting might be troublesome.

I tried to use:

df[(df['c1']==x1) or (df['c2']==x2)]

but it does not work.

user18243nanana
  • 139
  • 3
  • 9

1 Answers1

13

You need to use the logical or symbol |

df[(df['c1'] == x1) | (df['c2'] == x2)]

For and, you would need to use &

df[(df['c1'] == x1) & (df['c2'] == x2)]
Alexander
  • 105,104
  • 32
  • 201
  • 196
  • 2
    Really it's the bitwise or symbol, but the logical `or` can't be overloaded due to short-circuiting problems, so Pandas had to use `|` instead. – user2357112 Aug 16 '16 at 03:10