Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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.

1 Answer

0 votes
by (41.4k points)

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

columnsTitles=["B","A"]

df=df.reindex(columns=columnsTitles)

Browse Categories

...