How to swap two DataFrame columns?

PythonPandas

Python Problem Overview


In MATLAB, to swap the first and second columns of a table A, one would do this1

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

Is there a similarly convenient way to do this if A were a pandas DataFrame instead?

1 MATLAB uses 1-based indexing.

Python Solutions


Solution 1 - Python

pandas has reindex method that does it. You just need to give a list with the column names in the order you wish:

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

Cheers

Solution 2 - Python

A slight variant on 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]:
          a         b         c
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]:
          b         a         c
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 change the column order without changing the column contents then 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]:
          b         a         c
0 -0.200654 -0.682446 -1.609470
1  0.806378 -1.998113  1.252384
2  3.774708 -0.250359  1.100771

Solution 3 - Python

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 per Ivan's suggestions.

Solution 4 - Python

In my case, I have over 100 columns in my data frame. So instead list all columns, I wrote a short function to just 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

Solution 5 - Python

I finally settled for this:

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

It's far less convenient than the MATLAB version, but I like the fact that it does not require creating temporary variables.

Solution 6 - Python

If you have multiple columns and performance and memory are not an issue, 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)

Solution 7 - Python

I would use:

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

Solution 8 - Python

This works for me in Python 3.x:

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

Remember df = P.iloc[:, [1, 0] + range(2, P.shape[1])] wont work and will give the error:

TypeError: can only concatenate list (not "range") to list

Solution 9 - Python

colList = list(df.columns)
colList[0], colList[1] =  colList[1], colList[0]
df = df[colList]

Solution 10 - Python

For Dataframes in python, Considering that you have given the 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]]

Solution 11 - Python

column swap

import pandas as pd

df = pd.read_csv('/Users/parent/Desktop/Col_swap.csv')

print(df)

columns_titles = ["A","B","C","E"]

df_reorder=df.reindex(columns=columns_titles)

df_reorder.to_csv('/Users/parent/Desktop/col_reorder1.csv', index=False)

print(df_reorder)

Output:

    B   A   C   E
0  c1  a1  b1  d1
1  c2  a2  b2  d2


    A   B   C   E
0  a1  c1  b1  d1
1  a2  c2  b2  d2

Solution 12 - Python

You can use this easily

columns_titles = ["D","C","B","A"]

and then

df=df[column_titles]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkjoView Question on Stackoverflow
Solution 1 - Pythonaspire57View Answer on Stackoverflow
Solution 2 - PythonEdChumView Answer on Stackoverflow
Solution 3 - PythonacushnerView Answer on Stackoverflow
Solution 4 - PythonYingjian PanView Answer on Stackoverflow
Solution 5 - PythonkjoView Answer on Stackoverflow
Solution 6 - PythonLuca MastrostefanoView Answer on Stackoverflow
Solution 7 - PythonAturSamsView Answer on Stackoverflow
Solution 8 - PythonUmair NasirView Answer on Stackoverflow
Solution 9 - PythonDeepak BishtView Answer on Stackoverflow
Solution 10 - Pythonnityanarayan44View Answer on Stackoverflow
Solution 11 - PythonSriniView Answer on Stackoverflow
Solution 12 - PythonJordyView Answer on Stackoverflow