'How to sort values in pivot table by values names

Could you, please, help me with sorting the names of values?

I have the code which forms the Table 1:

df1 = pd.pivot(data=df_selected, index='source', columns='ds', values=['y','percent_diff'])
df1 = df1.swaplevel(0,1, axis=1).sort_index(axis=1, ascending=False)#.reset_index()
df1 = df1.sort_values([df1.columns[0]], ascending=False)

So, (2022-02-20, y), (2022-02-20, percent) are columns in this table in python and source is an index.

And I send the extracted data of my dataset, which was made by the code: enter image description here

I want to swap y and percent_diff. Please, help me with that.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source