My data frame is the following:
Advertiser Product Price Company1 A 10 Company1 A 10 Company1 B 8 Company2 C 5 Company3 D 3
My current function is:
top_5_products = df.groupby(['Advertiser'])['Product'].value_counts(ascending = False).head(5)
It outputs the following:
Advertiser Product Company 1 A 2 B 1 Company 2 C 1 Company 3 D 1
What is the best way to modify my function to give me the sum of the price as well? Example:
Advertiser Product Total Price Company 1 A 2 20 B 1 8 Company 2 C 1 5 Company 3 D 1 3
I have looked at the .agg method but I’m lacking examples that use different columns. (I’m also not sure if that’s the best way to go about it) Thanks!
Edit***
df.groupby(['Advertiser', 'Product']).agg({'Product': 'count', 'Price': 'sum'}).head(5)
doesn’t work as it is no longer sorted…
Advertisement
Answer
Someone commented with the answer, but it disappeared shortly after. Thankfully I saw it and it works. Thank you!
The following code works:
df.groupby(['Advertiser','Product']).agg({'Product': 'count', 'Price': 'sum'}).head(5)
EDIT*** This does not work as it no longer sorts.