Use the syntax DataFrame[["column1", "column2", "column3"]] with the column names in the desired order to reorder the columns.
DataFrame[["column1", "column2", "column3"]]
print(df)
Output:
A B C 0 1 4 7 1 2 5 8 2 3 6 9
df = df[["C", "A", "B"]] print(df)
C A B 0 7 1 4 1 8 2 5 2 9 3 6
pandas.DataFrame.reindex()
Call pandas.DataFrame.reindex(columns=column_names) with a list of the column names in the desired order as column_names to reorder the columns.
pandas.DataFrame.reindex(columns=column_names)
column_names
Last updated 5 years ago
column_names = ["C", "A", "B"] df = df.reindex(columns=column_names) print(df)