Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reverse dataframe's rows' order with pandas [duplicate]

How can I reverse the order of the rows in my pandas.dataframe?

I've looked everywhere and the only thing people are talking about is sorting the columns, reversing the order of the columns...

What I want is simple :

If my DataFrame looks like this:

   A      B     C
 ------------------
  LOVE    IS   ALL
  THAT   MAT   TERS

I want it to become this:

   A      B     C
 ------------------
  THAT   MAT   TERS
  LOVE    IS   ALL

I know I can iterate over my data in reverse order but that's not what I want.

like image 317
valentin Avatar asked Feb 06 '16 11:02

valentin


People also ask

How do I reverse the order of rows in pandas DataFrame?

Reversing the rows of a data frame in pandas can be done in python by invoking the loc() function. The panda's dataframe. loc() attribute accesses a set of rows and columns in the given data frame by either a label or a boolean array.

Does pandas drop duplicates keep first?

Only consider certain columns for identifying duplicates, by default use all of the columns. Determines which duplicates (if any) to keep. - first : Drop duplicates except for the first occurrence.


1 Answers

Check out http://pandas.pydata.org/pandas-docs/stable/indexing.html

You can do

reversed_df = df.iloc[::-1]
like image 76
Mike Graham Avatar answered Oct 16 '22 17:10

Mike Graham