A small version of acushner's answer:
# get a list of the columns col_list = list(df) # use this handy way to swap the elements col_list[0], col_list[1] = col_list[1], col_list[0] # assign back, the order will now be swapped df.columns = col_list
Example:
In [39]: df = pd.DataFrame({'a':randn(3), 'b':randn(3), 'c':randn(3)}) df Out[39]: abc 0 -0.682446 -0.200654 -1.609470 1 -1.998113 0.806378 1.252384 2 -0.250359 3.774708 1.100771 In [40]: col_list = list(df) col_list[0], col_list[1] = col_list[1], col_list[0] df.columns = col_list df Out[40]: bac 0 -0.682446 -0.200654 -1.609470 1 -1.998113 0.806378 1.252384 2 -0.250359 3.774708 1.100771
UPDATE
If you just want to reorder columns without changing the contents of the column, you can reindex using fancy indexing:
In [34]: cols = list(df) cols[1], cols[0] = cols[0], cols[1] cols Out[34]: ['b', 'a', 'c'] In [35]: df.ix[:,cols] Out[35]: bac 0 -0.200654 -0.682446 -1.609470 1 0.806378 -1.998113 1.252384 2 3.774708 -0.250359 1.100771
Edchum
source share