Reverse pandas DataFrame in Python (3 Examples)

 

In this article you’ll learn how to reverse the rows and columns of a DataFrame in Python programming.

The tutorial consists of the following information:

Let’s dive right in!

 

Creation of Exemplifying Data

To create an example DataFrame, we have to import the pandas software library:

import pandas as pd                                # Load pandas

Next, we can create some example data using the DataFrame function of the pandas library as shown below:

data = pd.DataFrame({'x1':list(range(1, 6)),       # Create example data
                     'x2':list(range(9, 4, - 1))})
print(data)                                        # Print example data
#    x1  x2
# 0   1   9
# 1   2   8
# 2   3   7
# 3   4   6
# 4   5   5

Have a look at the previous output – We have created a DataFrame with five rows and two columns x1 and x2.

 

Example 1: Reverse Ordering of DataFrame Rows

In this first example, I’ll explain how to reorder the rows of a DataFrame in Python.

For this, we can use the Python code that you can see below:

data_rev1 = data[::-1]                             # Reverse order of rows
print(data_rev1)                                   # Print updated data
#    x1  x2
# 4   5   5
# 3   4   6
# 2   3   7
# 1   2   8
# 0   1   9

As you can see, the values of the bottom of our DataFrame have been moved to the top and the other way around.

However, you can also see that the indices of our data frame have been reversed as well.

In the next example, I’ll show how to keep the indices in a reversed DataFrame. So keep on reading!

 

Example 2: Reverse Ordering of DataFrame Rows & Reset Index

Example 2 demonstrates how to keep the index order in a reversed DataFrame using the reset_index function in Python.

Have a look at the Python code below:

data_rev2 = data[::-1].reset_index(drop = True)    # Reset index
print(data_rev2)                                   # Print updated data
#    x1  x2
# 0   5   5
# 1   4   6
# 2   3   7
# 3   2   8
# 4   1   9

The values in our reversed DataFrame are the same as in Example 1. However, this time the indices in the reversed data table are still ranging from 0 to the number of rows.

 

Example 3: Reverse Ordering of DataFrame Columns

So far, we have only modified the positioning of the rows of our DataFrame. However, it is also possible to rearrange the variables in our data matrix.

The following Python syntax explains how to reverse the columns in a DataFrame:

data_rev3 = data[data.columns[::-1]]               # Reverse order of columns
print(data_rev3)                                   # Print updated data
#    x2  x1
# 0   9   1
# 1   8   2
# 2   7   3
# 3   6   4
# 4   5   5

The previous output shows the result of this example: We have switched the positions of our two variables x1 and x2.

 

Video & Further Resources

Do you want to know more about DataFrames in Python? Then you might want to have a look at the following video of Joe James’ YouTube channel. He explains how to manipulate pandas DataFrames in more detail.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Additionally, you might want to have a look at the related tutorials of this homepage. I have released several other Python tutorials already:

 

In this tutorial you have learned how to reorder the lines and variables of DataFrames in the Python programming language. In case you have any additional questions, let me know in the comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top