I have a pivot table where the rows are sorted by date, then columns are in alphabetical order.
I would like to see the column order differently. For example if location is showing a value in the first row then I would like that to be the first column.
I basically tried every version of the code below using sort_values:
df1 = pd.pivot_table(df1, values = 'num', index='date', columns = 'location_name',aggfunc = "sum")
result = df1.sort_values(('date'), ascending=False)
// +-----------+---+---+---+---+
// | date | a | b | c | d |
// +-----------+---+---+---+---+
// | 7/31/2021 | | | 1 | |
// | 8/1/2021 | | | 1 | |
// | 8/2/2021 | | | 1 | |
// | 8/3/2021 | | | | 2 |
// | 8/4/2021 | | | | 2 |
// | 8/5/2021 | | | | 2 |
// | 8/6/2021 | | | | 2 |
// | 8/7/2021 | | | | 2 |
// | 8/8/2021 | 3 | | | |
// | 8/9/2021 | 3 | | | |
// | 8/10/2021 | 3 | | | |
// +-----------+---+---+---+---+
So the order of the columns above should be c,d,a,b in that order with the date order staying the same.
Any ideas on the best way to do this?