Open In App

How to Reverse Row in Pandas DataFrame?

In this article, we will learn how to reverse a row in a pandas data frame using Python

With the help of Pandas, we can perform a reverse operation by using loc(), iloc(), reindex(), slicing, and indexing on a row of a data set. 



Creating Dataframe

Let’s create a simple data frame with a dictionary, say column names are: ‘Income’, ‘Expenses’, ‘Profit’.




# Import pandas package
import pandas as pd
 
# Define a dictionary containing employee data
data = {'Income': [150000, 13000, 11000, 11000],
        'Expenses': [10000, 11000, 7000, 50000],
        'Profit': [5000, 2000, 4000, 6000]
        }
 
 
# Convert the dictionary into DataFrame
dataframe = pd.DataFrame(data)
 
# Observe the result
dataframe

Output:



 

Using iloc() function to Reverse Row

Reversing the rows of a data frame in pandas can be done in python by invoking the iloc() function.  Let us know how to reverse the rows of a data frame in pandas.

Syntax: DataFrame.iloc[]

Parameters: Index Position: Index position of rows in integer or list of integer.

Return: Data frame or Series depending on parameters




# Reverse rows using iloc() Function
Data_reverse_row_1 = dataframe.iloc[::-1]
 
# Observe the result
Data_reverse_row_1

Output:

reversed database

Using loc() function to Reverse Row

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.

Syntax: DataFrame.loc()

Parameter : None

Returns : Scalar, Series, DataFrame




# Reverse rows using iloc() Function
Data_reverse_row_2 = dataframe.loc[::-1]
 
# Observe the result
Data_reverse_row_2

Output:

 

Note: .loc() and .iloc() use the indexers to select for indexing operators.

Using reindex() function to Reverse Row

Reverse rows of the data frame using reindex() Function. The pandas dataframe.reindex() function concatenates the dataframe to a new index with optional filling logic, placing NA/NaN at locations that have no value in the previous index.

Syntax: DataFrame.reindex(index=None)

Parameter : index, columns : New labels / index to conform to. Preferably an Index object to avoid duplicating data

Returns : reindexed : DataFrame




# reversing a DataFrame
# retrieving row by reindex method
df.reindex(index=dataframe.index[::-1])

Output:

 

Using dataframe indexing to Reverse Row

Reverse rows using data frame indexing in python. In Python, we can set the index of a dataframe in reverse. In this method, we create a Python list and pass it’s an index to the dataframe() function’s index parameter. Let’s implement this through Python code.

Syntax: DataFrame[start:end:slicing]




# Reverse slicing columns in data frame
dataframe[::-1]

Output:

 

Using the reset_Index() function to Reverse Row

Here we use the reset_index() function to reset the index for the entire database and also pass Drop=True to drop all old indices.




# Here we are just resetting the indexing for the entire database
# and reversing the database.
 
d = dataframe.loc[::-1].reset_index(drop=True).head()
print(d)

Output:

reversed database


Article Tags :