Pandas How To Uncategorized How to move column order

How to move column order

In this article I show how to move column order in Pandas dataframe.

To move the order of columns in a pandas DataFrame, you can use the reindex method or the moveto method of the columns attribute:

import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})

# using reindex
df1 = df.reindex(columns=['B', 'C', 'A'])


# or using moveto
cols = df.columns.tolist()
cols.insert(0, 'B')
df2 = df.reindex(columns=cols)


As you can see the order of the columns has been changed.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post