How to change the order of columns of a dataframe?

I am asking for alternative solutions to change the order of columns in a Pandas dataframe using Python, without altering the data. I want to know if there are built-in functions or Python libraries that can automate the process or any clever tricks or workarounds to achieve this more efficiently. The approach I used to change the order of columns is:

I change the order of columns by simply creating a new DataFrame with the desired column order.

1 Like

To reorder columns in a Pandas dataframe, I use the .iloc() method and in this method, you provide a list of integers that represent the new order of columns. This provides greater flexibility as you can rearrange both rows and columns using the same method. Here is an example code:

In this example, the iloc method is used to reorder the columns of a dataframe by specifying the new order of columns as [1, 2, 0]. This creates a new dataframe with the columns in the desired order.