change the column position in pandas code example
Example 1: change column name df
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
>>> df.rename(columns={"A": "a", "B": "c"})
a c
0 1 4
1 2 5
2 3 6
Example 2: how to change column name in pandas
print(df.rename(columns={'A': 'a', 'C': 'c'}))
Example 3: pandas reorder columns by name
df.columns
Index(['A', 'B', 'C', 'D'],dtype='***')
new_col = ['D','C','B','A']
df = df[new_col]
df.columns
Index(['D', 'C', 'B', 'A'],dtype='***')
Example 4: 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']