0
$\begingroup$

I have found out the most profitable products in my dataframe by using:

df.groupby('ProductName')['ProfitPerOrder'].sum().sort_values().tail()

It gives me the output below. each ProductName has a ProductCategory. How do I display the category next to the product name in the output below?

ProductName
WFS Shoes           29033.887659
RDL Suit            45845.253318
Tennis Suit         46848.513342
Davenport Shoes    127103.915707
Halter Dress       314155.742025
Name: ProfitPerOrder, dtype: float64
$\endgroup$
1
  • 1
    $\begingroup$ Add more columns $\endgroup$
    – Aditya
    Mar 3 '18 at 2:34
1
$\begingroup$

Add more columns when you are doing group by in the first parentheses..

First we should understand why it's giving this result..

It's similar to Sql,we are applying an aggregate function on a grouped by value, That's why it's giving only one value,

If you want to have further information, Add the columns name in the first parentheses in a $list$

It will be something like this

df.groupby([Col1,Col2...,Coln])(another col).sum().sort_values().tail()

Edit -1

Recently enough, I came across that we can cascade group by together by playing smart and using the .join(second group by(). aggregate ())

Here's the working example (ignore the column names...it's relevant to my dataset)

df.groupby('Year_of_Release')[['Global_Sales']].sum().join( df.groupby('Year_of_Release')[['Name']].count())

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.