Skip to content
Advertisement

How do I do a groupby in python to split between orders?

I have a dataframe that shows order listing. How do I use it to find the number of orders that contain spicy food?

JavaScript

Using this code gives me 2 Yes and 2 No, but it should actually be 2 Yes and 1 No as order 1001 is duplicated. Thank you.

JavaScript

I would like to get an output that shows the number of orders that contains spicy food.

JavaScript

Advertisement

Answer

There’s perhaps a simpler way, but this works.

First group by order and spicy counts, to get the count of spicy for each order. Then sort by spicy and drop duplicates by order number (removes the ‘No’ in spicy column if a yes exists for that order). Then group by Spicy again and count to get the counts.

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement