Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse DataFrame column order

I want to simply reverse the column order of a given DataFrame.

My DataFrame:

data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],     'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions', 'Lions', 'Lions'],     'wins': [11, 8, 10, 15, 11, 6, 10, 4],     'losses': [5, 8, 6, 1, 5, 10, 6, 12]} football = pd.DataFrame(data, columns=['year', 'team', 'wins', 'losses']) 

Actual output:

   year     team  wins  losses 0  2010    Bears    11       5 1  2011    Bears     8       8 2  2012    Bears    10       6 3  2011  Packers    15       1 4  2012  Packers    11       5 5  2010    Lions     6      10 6  2011    Lions    10       6 7  2012    Lions     4      12 

I thought this would work but it reverses the row order not column order:

football[::-1]  

I also tried:

football.columns = football.columns[::-1] 

but that reversed the column labels and not the entire column itself.

like image 785
Boosted_d16 Avatar asked Jan 07 '15 10:01

Boosted_d16


People also ask

How do you reverse the order of a series in python?

A Python list has a reverse function. The [::-1] slice operation to reverse a Python sequence. The reversed built-in function returns a reverse iterator.

How do you flip a Dataframe?

Use the T attribute or the transpose() method to swap (= transpose) the rows and columns of pandas. DataFrame . Neither method changes the original object but returns a new object with the rows and columns swapped (= transposed object).

How do you arrange a Dataframe in descending order?

We can sort it by using the dataframe. sort_index() function. Alternatively, you can sort the index in descending order by passing in the ascending=False the argument in the function above.


2 Answers

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.

like image 185
Alex Riley Avatar answered Sep 29 '22 11:09

Alex Riley


Note: As of Pandas v0.20, .ix indexer is deprecated in favour of .iloc / .loc.

Close to EdChum's answer... but faster:

In [3]: %timeit football.ix[::,::-1] 1000 loops, best of 3: 255 µs per loop  In [4]: %timeit football.ix[::,football.columns[::-1]] 1000 loops, best of 3: 491 µs per loop 

Also notice one colon is redundant:

In [5]: all(football.ix[:,::-1] == football.ix[::,::-1]) Out[5]: True 

EDIT: a further (minimal) improvement is brought by using .loc rather than .ix, as in football.loc[:,::-1].

like image 29
Pietro Battiston Avatar answered Sep 29 '22 09:09

Pietro Battiston