I have been trying to sort a pivot table generated by pandas. This where I am at so far:
pv=pd.pivot_table(data,index=["TIN"], columns=["Tax Type"], values=["FileFreq"], aggfunc=[np.sum], margins=True)
A subset of the data I am working on is as follows:
TIN | Tax Type | FileFreq |
---|---|---|
875-729 | CWT | 1 |
875-729 | CWT | 1 |
875-729 | CWT | 1 |
875-729 | CWT | 1 |
875-729 | EWT | 1 |
875-729 | EWT | 1 |
875-729 | EWT | 1 |
875-729 | EWT | 1 |
875-729 | EWT | 1 |
138-075 | CWT | 1 |
138-075 | CWT | 1 |
138-075 | CWT | 1 |
138-075 | CWT | 1 |
138-075 | EWT | 1 |
138-075 | EWT | 1 |
138-075 | EWT | 1 |
138-075 | EWT | 1 |
138-075 | EWT | 1 |
138-075 | EWT | 1 |
138-075 | EWT | 1 |
944-507 | CWT | 1 |
944-507 | CWT | 1 |
944-507 | EWT | 1 |
944-507 | EWT | 1 |
944-507 | EWT | 1 |
944-507 | EWT | 1 |
A snapshot of the resulting pivot table I got from the whole dataset is given below:
Tax Type Sum of File Freq
TIN | CWT | EWT | All |
---|---|---|---|
130-344 | 11 | 7 | 18 |
138-075 | 12 | 9 | 21 |
151-387 | 12 | 12 | 24 |
230-328 | 12 | 12 | 24 |
257-018 | 12 | 12 | 24 |
484-606 | 9 | 9 | 18 |
597-665 | 10 | 10 | 20 |
875-729 | 10 | 12 | 22 |
894-747 | 12 | 12 | 24 |
944-507 | 6 | 11 | 17 |
All | 106 | 106 | 212 |
I would like to sort from lowest to highest first by the CWT column (one table), then by the EWT column (another table). Have been trying different recommendations from similar questions posted here in SO but to no avail. One of those I tried is
pvsort = pv.reindex(pv['FileFreq'].sort_values(by=['CWT']).index)
but I am getting an error Thanks in advance for the help