How can I change the order of columns in a Pandas DataFrame?

I’m currently working with a Pandas dataframe, and I need to change the order of the columns. How can I do that?

Here’s an example of what my dataframe looks like:


import pandas as pd

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

And here’s the order of the columns:

Index(['A', 'B', 'C'], dtype='object')

I need to change the order of the columns to ['C', 'A', 'B']. Can anyone show me how to do that using pandas?

Here’s my current attempt:

df = df[['C', 'A', 'B']]

However, this doesn’t seem to be working. Can anyone help me figure out what I’m doing wrong and how to fix it? Thanks in advance for any help you can provide!