I would like to re-index the columns of a dataframe by sorting them by both alphabetical order and numerical order.
In my dataframe df, my columns are named:
Round 1 Position, Round 2 Position, Round 1 Score, Round 2 Score, Round 1 Price, Round 2 Price, ..., Round 10 Position, Round 10 Score, Round 10 Price
I would like them to be ordered as follows:
Round 1 Position, Round 1 Price, Round 1 Score, Round 2 Position, Round 2 Price, Round 2 Score, Round 3 Position, Round 3 Price, Round 3 Score, ..., Round 10 Position, Round 10 Price, Round 10 Score
I have tried the following:
df.reindex(sorted(df.columns, key=lambda x: float(x[1:])), axis=1)
but had no luck as the column name was a string.
Thank you for taking the time to read through my question and help me. Much appreciated!