How to swap two columns of a DataFrame?

In MATLAB, to replace the first and second columns of table A you can do this 1

 A = A(:, [2 1 3:end]); 

Is there an equally convenient way to do this if A was a pandas DataFrame instead?

1 MATLAB uses 1- based indexing.

+28
python pandas
source share
8 answers

pandas has a reindex method that does this. You just need to specify a list with column names in the order you want:

 columnsTitles=["B","A"] df=df.reindex(columns=columnsTitles) 

Greetings

+52
source share

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 
+15
source share
 c = A.columns A = A[c[np.r_[1, 0, 2:len(c)]]] 

or, even easier:

 A[[c[0], c[1]]] = A[[c[1], c[0]]] 

* edit: fixed by Ivan.

+4
source share

I finally decided:

 A = A.iloc[:, [1, 0] + range(2, A.shape[1])] 

This is much less convenient than the MATLAB version, but I like the fact that it does not require the creation of temporary variables.

+3
source share

If you have multiple columns and performance and memory are not a problem, you can simply use this function:

 def swap_columns(df, c1, c2): df['temp'] = df[c1] df[c1] = df[c2] df[c2] = df['temp'] df.drop(columns=['temp'], inplace=True) 
+2
source share

I would like to use:

 end = df.shape[1] # or len(df.columns) df.iloc[:, np.r_[1, 0, 2:end] 
+1
source share

In my case, there are more than 100 columns in my data frame. Therefore, instead of listing all the columns, I wrote a short function to switch two columns.

 def df_column_switch(df, column1, column2): i = list(df.columns) a, b = i.index(column1), i.index(column2) i[b], i[a] = i[a], i[b] df = df[i] return df 
+1
source share

For Dataframes in python, considering you gave 2 columns, then:

 #df is your data frame col1='c1' col2='c2' df = df[[col1 if col == col2 else col2 if col == col1 else col for col in df.columns]] 
0
source share

All Articles