0

I'm struggling with the usage of pivot tables for pandas.

I have a dataframe with 3 columns: Company name, date and number of orders.

I'm trying to pivot the dataframe in such a way that I'd be able to see top 10 biggest customers.

So for example, lets say I have

name | date | orders
John | 2017 | 100
John | 2018 | 200
John | 2019 | 300
Mary | 2017 | 50
Mary | 2018 | 50
Mary | 2019 | 1000

I want to pivot this in a way where I'd be able to see it like this

name
date  2017 | 2018 | 2019
John  100  | 200  | 300
Mary  50   | 50   | 1000

And sorted by total amount of orders, so in this case, Mary should go first because she has 1100 orders in total.

I cannot do the sorting in groups, it's outputting Mary 1000 1st, John 300 2nd and John 200 3rd (to give an example)

Also, maybe a bit unrelated but I also noticed that when pivoting, it's throwing the results in scientfic notation (despite the df displaying the numbers without it) and if I disable SciNot, it's converting the values to floats, any ideas as to why that is?

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
Dasphillipbrau
  • 524
  • 2
  • 8
  • 17

1 Answers1

0

Try:

df_out = df.pivot('name','date')['orders']
df_out = df_out.assign(sortkey = df_out.sum(axis=1))\
               .sort_values('sortkey', ascending=False)\
               .drop('sortkey', axis=1)

Output:

date  2017  2018  2019
name                  
Mary    50    50  1000
John   100   200   300
Scott Boston
  • 147,308
  • 15
  • 139
  • 187