qwertylpc
qwertylpc

Reputation: 2106

Pythonic Way to have multiple Or's when conditioning in a dataframe

Basically instead of writing

data[data['pos'] == "QB" | data['pos'] == "DST" | ...]

where there are many cases I want to check

I was trying to do something similar to this What's the pythonic method of doing multiple ors?. However, this

data[data['pos'] in ("QB", "DST", ...)]

doesn't work.

I read the documentation here http://pandas.pydata.org/pandas-docs/stable/gotchas.html but I'm still having issues.

Upvotes: 2

Views: 266

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90889

What you are looking for is Series.isin . Example -

data[data['pos'].isin(("QB", "DST", ...))]

This would check if each value from pos is in the list of values - ("QB", "DST", ...) . Similar to what your multiple | would do.

Upvotes: 2

Related Questions