Six ways to reverse pandas dataframe

This post was originally published here

In this post we will learn how to reverse pandas dataframe. We start by changing the first column with the last column and continue with reversing the order completely. After we have learned how to do that we continue by reversing the order of the rows. That is, pandas data frame can be reversed such that the last column becomes the first or such that the last row becomes the first.

First, we create some example data using normal and binomial from numpy.random.  Using normal we create two different response time variables (response times are often used in experimental psychology) and binomial is used to create response accuracy (also often used in psychology experiments). We will use pandas to create a dataframe from the data. Note, the simulation of data is not intended to be used for anything else than for us to have data to create a pandas dataframe.

import pandas as pd
import numpy as np

distracted = [1 for i in xrange(15)]
normal_perf = [0 for i in xrange(15)]

sex = ['Male' if i%2 == 0 else 'Female' for i in xrange(30)]

rt_distracted = np.random.normal(1660, 100, 15)
rt_normal_perf = np.random.normal(1054, 97, 15)

accuracy_distracted = np.random.binomial(1, .79, 15)
accuracy_normal_perf = np.random.binomial(1, .87, 15)

rt = np.concatenate((rt_distracted, rt_normal_perf))
acc = np.concatenate((accuracy_distracted, accuracy_normal_perf))

subject_id = [i for i in xrange(1,31)]

data = {'Sub_id':subject_id, 'Condition':distracted, 'RT':rt, 
         'Accuracy':acc, 'Gender':sex}
data_frame = pd.DataFrame(data)

Reversing by column

First are we going to change places of the first (“Accuracy) and last column (“Sub_id”).
We start creating a list of the column names and swapping the first item to the last:

columns = data_frame.columns.tolist()
columns = columns[-1:] + columns[:-1]

Then we continue with using the created list and the columns in the dataframe will change places.

data_frame = data_frame[columns]

We can sort our dataframe ascending again (i.e., alphabetical):

data_frame = data_frame.sort_index(axis=1 ,ascending=True)

You can also reverse pandas dataframe completely. The second line (columns[::-1]) reverse the list of column names. Third, the dataframe is reversed using that list.

columns = data_frame.columns.tolist()
columns = columns[::-1]
data_frame = data_frame[columns]

Reverse by row

Pandas dataframe can also be reversed by row. That is, we can get the last row to become the first.  We start by re-order the dataframe ascending:

data_frame = data_frame.sort_index(axis=1 ,ascending=True)

First, we will use iloc which is integer based.

data_frame = data_frame.iloc[::-1]

However, we can also use sort_index by using the axis 0 (row). This will, of course, reverse the dataframe back to the one we started with.

data_frame = data_frame.sort_index(ascending=True, axis=0)

Lastly, we can use the method reindex to reverse by row. This will sort Pandas DataFrame reversed. Last element will be first.

data_frame = data_frame.reindex(index=data_frame.index[::-1])

That was it; six ways to reverse pandas dataframe. You can go to my GitHub-page to get a Jupyter notebook with all the above code and some output: Jupyter notebook.

The post Six ways to reverse pandas dataframe appeared first on Erik Marsja.

Related Posts

You have two jobs Welcome to FictionalSoft! I hope your first week is going well? Great. As you start to find your feet, I want to make sure we’re have a sha...
psutil 5.4.0 with AIX support is out After a long time psutil finally adds support for a brand new exotic platform: AIX! Honestly I am not sure how many AIX Python users are out there (I ...
Gynvael’s Mission 11 (en): Python bytecode reverse-engineering Gynvael Coldwind is a security researcher at Google, who hosts weekly livestreams about security and programming in Polish and English). As part of th...
Leaving HPE For the past two years I have been employed by Hewlett Packard Enterprise to work on the various tools, libraries, and frameworks that make up the ope...