How to cut pandas DataFrame according to disjunction statement (logical "or")?

I would like to slice a pandas.DataFramethat satisfies condition A or condition B. Most of the search results show how to cut the data frame with and . So I'm wondering if it is possible to use the "or" operator without converting (A and B) to (not (not A, not B))? Because sometimes there are many “or” conditions, and conversion can be difficult.

I tried using:

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

but that will not work.

+4
source share
1 answer

You need to use a boolean or character |

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

and &

df[(df['c1'] == x1) & (df['c2'] == x2)]
+5

Source: https://habr.com/ru/post/1651346/


All Articles