long time XLS user still new to Python/Pandas.I'm tying to automate a report for some price curves and I can't quite manage to get the Pivot in the format I'm looking for. Thanks in advance for your help!
I have a dataframe in this format:
data = [['AAA',1,11,1,],['AAA',2,12,2],['AAA',3,13,3],
['BBB',1,21,5],['BBB',2,22,6],['BBB',3,23,7],
['CCC',1,31,9],['CCC',2,32,10],['CCC',3,33,11]]
df = pd.DataFrame(data, columns = ['Curve', 'Tenor','Price','Change'])
print(df)
Curve Tenor Price Change
0 AAA 1 11 1
1 AAA 2 12 2
2 AAA 3 13 3
3 BBB 1 21 5
4 BBB 2 22 6
5 BBB 3 23 7
6 CCC 1 31 9
7 CCC 2 32 10
8 CCC 3 33 11
I pivoted the df as follows and the result looks like this:
df2 = pd.pivot_table(df, values=['Price','Change'], index='Tenor',columns='Curve', aggfunc=np.mean)
Change Price
Curve AAA BBB CCC AAA BBB CCC
Tenor
1 1 5 9 11 21 31
2 2 6 10 12 22 32
3 3 7 11 13 23 33
I'd like it to change the order of the column grouping such that it looked like
AAA BBB CCC
Tenor Price Change Price Change Price Change
1 11 1 5 21 31 9
2 12 2 6 22 32 10
3 13 3 7 23 33 11
In XLS you'd just move the fields around physically in the Pivot. I'm sure its just as trivial here too :) but no matter how I google I can't seem to find a solution. Thanks again for your help! Any and all assistance appreciated. Cheers.