I have already sorted the dataframe(dfDogNew) based on product_category and quantity_sold. Now I want to access the first two most sold product in each product category, how can I achieve this? I have written a for loop to access them, but the system tells me there is a key error, does anyone can help me on this? Thank you! Also, if it only has one product in the dfDogNew, then it will only return one row, I assume there should be no difference if I set the slicing as [:2], pandas will automatically pass to the next category there is only 1 product in the previous cstegory?
I will attach my for loop code below:
for i in product_category:
for g in dfDogNew['product_category']:
if i == g:
finaldf = dfDogNew[dfDogNew[g]].iloc[:2]
else:
pass
sorted dataframe based on product_category and quantity_sold
for loop attempt to access the first two elements out of each category
I have listed all my attempts in the previous description.