I have (for example) this DataFrame:
COLUMN1 COLUMN2 VALUE
0 0102 1020 1
1 0102 1220 8
2 0102 1210 2
3 0103 1020 1
4 0103 1210 3
5 0103 1222 8
6 0104 1020 3
7 0104 1120 2
(In reailty, it's ~9000 rows long.)
From this, I create the pivot table where indexes are COLUMN1, columns are COLUMN2, and the values are from VALUES, filled by 0 where NaN.
COLUMN2 1020 1120 1210 1220 1222
COLUMN1
0102 1 0 2 8 0
0103 1 0 3 0 8
0104 3 2 0 0 0
I have to sort this pivot by the grand total of rows, then by the grand total of columns. That would look like this:
COLUMN2 1220 1222 1020 1210 1120| (GT)
COLUMN1 | HIGHEST
0103 0 8 1 3 0| (12) |
0102 8 0 1 2 0| (11) |
0104 0 0 3 0 2| (5) V
--------------------------------------
(GT: 8 8 5 5 2)
HIGHTEST----------------------------> LOWEST
Is there a way to do this? I have tried creating the pivot by importing the indexes and columns as lists, sorted in the order I would like them to appear, but pandas seems to automatically sort them A-Z when creating the table.
Code for the example:
import pandas as pd
exampledata=[['0102','1020',1],['0102','1220',8],['0102','1210',2],
['0103','1020',1],['0103','1210',3], ['0103','1222',8],
['0104','1020',3],['0104','1120',2]]
df = pd.DataFrame(exampledata,columns=['COLUMN1','COLUMN2','VALUE'])
print(df)
pivot = pd.pivot_table(df,
index='COLUMN1',
columns='COLUMN2',
values='VALUE',
aggfunc='sum',
fill_value=0)
print(pivot)