Reverse the rows of the dataframe in pandas python

Reverse the rows of the dataframe in pandas python can be done in by using iloc() function. Let’s see how to

  •          Reverse the rows of the dataframe in pandas

With examples

First let’s create a dataframe

import pandas as pd
import numpy as np

#Create a DataFrame
df1 = {
    'State':['Arizona AZ','Georgia GG','Newyork NY','Indiana IN','Florida FL'],
   'Score':[62,47,55,74,31]}

df1 = pd.DataFrame(df1,columns=['State','Score'])
print(df1)

df1 will be

Reverse the rows of the dataframe in pandas 1

 

Reverse the rows of the dataframe in pandas

iloc[::-1] reverses the rows of the dataframe by re arranging .

''' reverse the rows in pandas '''
df1.iloc[::-1]

so the resultant dataframe will be

Reverse the rows of the dataframe in pandas 2

 

p Reverse the rows of the dataframe in pandas python                                                                                                        n Reverse the rows of the dataframe in pandas python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.