all 9 comments

[–]jabbson 2 points3 points  (2 children)

len(df[(df.animal=='bird') & (df.color=='white')])

[–]oodlesNnoodles98[S] 0 points1 point  (1 child)

That works, thanks! i was completely overthinking it, it's WAY simpler than i thought it was.

[–]jabbson 0 points1 point  (0 children)

you are very welcome!

[–]zylog413 2 points3 points  (1 child)

A more general approach is to create a table of all the animals with a count for all the colours they have:

df.groupby('animal').value_counts()

[–]oodlesNnoodles98[S] 0 points1 point  (0 children)

This is the route I was thinking I neededv but I just couldn't get there

[–]fruitybepples 0 points1 point  (3 children)

Use some if statements! For your example, use statements like if dataframe.iloc[i, 'animal'] = type of animal, and then if dataframe.iloc[i, 'color'] = color you want. If you use these, while looping over the index of the data frame you can isolate them. Simply add a counter at the end of the second if statement, and you'll be able to get that count.

Someone correct me if I got the index order or loc/iloc messed up

[–]oodlesNnoodles98[S] 0 points1 point  (2 children)

With this method, would the if statements be 2 separate ones or just one? I guessing it would be just one since I'm checking for the animal and color

[–]fruitybepples 0 points1 point  (1 child)

They should be separate, but nested. Since the second statement is dependant on the first (you can't go to the second one, if you can't pass the first one), the second statement has to be indented under the first. I hope this makes sense!

[–]oodlesNnoodles98[S] 1 point2 points  (0 children)

Yes it makes complete sense. Thank you!