I'd like to calculate subtotals on this dataset:
Country Channel Genre Size Prev
UK Partners Blues 25 20
UK Stores Blues 15 10
UK Stores Rock 35 30
US Stores Rock 45 40
UK Partners Rock 55 50
US Partners Rock 65 60
UK Stores Blues 5 2
This solution calculates subtotals, but I need each level sorted differently. Specifically:
>>> columns = ['Country', 'Channel', 'Genre']
>>> sort = {'Country': 'Country', 'Channel': 'Size', 'Genre': 'Prev'}
>>> subtotal(data, columns, agg='sum', sort=sort)
Country Channel Genre Size Prev
0 UK Stores Blues 20 12
1 UK Stores Rock 35 30
2 UK Stores 55 42
3 UK Partners Blues 25 20
4 UK Partners Rock 55 50
5 UK Partners 80 70
6 UK 135 112
7 US Stores Rock 45 40
8 US Stores 45 40
9 US Partners Rock 65 60
10 US Partners 65 60
11 US 110 100
12 245 212
In this:
- The "Country" column is sorted in ascending order of Country name (UK before US)
- The "Channel" column is sorted (within Country) in ascending order of Size (UK Stores = 55 before UK Partners = 80)
- The "Genre" column is sorted (within Country & Genre) in ascending order of Prev (UK Stores Blues = 20 before UK Stores Rock = 35)
How can we do this efficiently?