Reverse DataFrame column order

A solution close to what you have already tried is to use:

>>> football[football.columns[::-1]]
   losses  wins     team  year
0       5    11    Bears  2010
1       8     8    Bears  2011
2       6    10    Bears  2012
3       1    15  Packers  2011
4       5    11  Packers  2012
5      10     6    Lions  2010
6       6    10    Lions  2011
7      12     4    Lions  2012

football.columns[::-1] reverses the order of the DataFrame’s sequence of columns, and football[...] reindexes the DataFrame using this new sequence.

A more succinct way to achieve the same thing is with the iloc indexer:

football.iloc[:, ::-1]

The first : means “take all rows”, the ::-1 means step backwards through the columns.

The loc indexer mentioned in @PietroBattiston’s answer works in the same way.

Leave a Comment