rearrange pandas df column order code example
Example 1: python change column order in dataframe
cols = df.columns.tolist()
cols = cols[-1:] + cols[:-1] #bring last element to 1st position
df = df.reindex(cols, axis=1)
Example 2: rearrange columns pandas
You could also do something like this:
df = df[['mean', '0', '1', '2', '3']]
You can get the list of columns with:
cols = list(df.columns.values)
The output will produce:
['0', '1', '2', '3', 'mean']