0

How do I extract the two different values (dog and cat) and then combine them under a new Dataframe

I tried

d = pd.DataFrame({'Animal':['cat', 'dog', 'bird', 'dog', 'bird', 'bird'],'Age':[1,3,5,3,4,2]})
df[(df['Animal'] == 'cat')&(df['Animal']=='dog')]

But when trying to locate 2 different values it only shows the top row "Animal Age"

I can find all of the dog values

df[(df['Animal'] == 'dog')]

but I'm not able to extract dog and cat rows at the same time from the Animal column

jayden
  • 21
  • 2

2 Answers2

0

The condition you are using to filter your results is & which in your case, both cat and dog do not happen on the same row. You need to use | (i.e., union).

df = pd.DataFrame({'Animal':['cat', 'dog', 'bird', 'dog', 'bird', 'bird'],'Age':[1,3,5,3,4,2]})
df[(df['Animal'] == 'cat')|(df['Animal']=='dog')]
0

Use or conditional, if you use and, both cat and dog has to be true. Which makes an empty dataframe:

df[(df['Animal'] == 'cat') | (df['Animal']=='dog')]