I have dataframe like in the below pic.
First; I want the top 2 products, second I need the top 2 products frequents per day, so I need to group it by days and select the top 2 products from products column, I tried this code but it gives an error.
df.groupby("days", as_index=False)(["products"] == "Follow Up").count()
You need to
groupby
over both days and products and then usesize
. Once you have done this you will have all the counts in thedf
you require.You will then need to sort both the
day
and the default0
column which now contains your counts, this has been created by resetting your index on the initialgroupby
.We follow the instructions in Pandas get topmost n records within each group to give your desired result.
A full example:
Setup:
Of course, you should rename the
0
column to something more reasonable but this is a minimal example.