0

Let's say have a giant dataframe documenting the number of animals in a zoo

[Animal] ... [number]

[cow] ... [3]

[fish] ... [6]

.

.

.

[pig] ... [5]

and I want to reduce this to look at n specific animals

then I would do something like this

df = df.loc[["Animal"]=="cow" | ["Animal"]=="pig" | ...]

for as many animals as I want

but how can I do this so for any given set

set = ["cow", "pig", .... ] 

something of the form

df = df.loc[["Animal"] == i for i in set]

but this would make the union (i.e. it would look for places where Animal = cow AND pig AND all everything else)

Is there a version where It will look for the intersection ( i.e. Animal = cow OR pig OR anything else)

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
  • related: https://stackoverflow.com/questions/17071871/select-rows-from-a-dataframe-based-on-values-in-a-column-in-pandas, are you trying to search for rows where 'Animal' is in some sort of list? If so then this is a dupe – EdChum Apr 04 '19 at 15:06
  • basically `df = df.loc[df['Animal'].isin(set)]` by the way don't use variable names that are python reserved words, using `set` shadows the type `set` – EdChum Apr 04 '19 at 15:08
  • Great, Thank you – Stephen Jackson Apr 04 '19 at 15:10

0 Answers0