[Solved] Sort the order of dataframe columns based on the values in the bottom row


You were close. Try this:

import pandas as pd

df = pd.DataFrame({'a': [1, 2, 3], 'b': [ 4, 5, 2], 'c': [2, 4, 5]})
print(df)

df = df[[x for _, x in sorted(zip(df.iloc[-1], df.columns), reverse=True)]]
print(df)

Starting DataFrame:

   a  b  c
0  1  4  2
1  2  5  4
2  3  2  5

Columns sorted by the values in the bottom row, in descending order:

  c  a  b
0  2  1  4
1  4  2  5
2  5  3  2

solved Sort the order of dataframe columns based on the values in the bottom row