We start by changing the first column with the last column and continue with reversing the order completely. After we learned how to do that we continue by reversing the order of the rows. First some data is created.
This version is now in Python 3 and have one added example code.
import pandas as pd
import numpy as np
distracted = [1 for i in range(15)]
normal_perf = [0 for i in range(15)]
sex = ['Male' if i%2 == 0 else 'Female' for i in range(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))
distracted.extend(normal_perf)
subject_id = [i for i in range(1,31)]
data = {'Sub_id':subject_id, 'Condition':distracted, 'RT':rt,
'Accuracy':acc, 'Gender':sex}
data_frame = pd.DataFrame(data)
#Print the first 5 rows
data_frame.head()
Sub_id | Condition | RT | Accuracy | Gender | |
---|---|---|---|---|---|
0 | 1 | 1 | 1692.560014 | 0 | Male |
1 | 2 | 1 | 1565.725442 | 1 | Female |
2 | 3 | 1 | 1829.502355 | 1 | Male |
3 | 4 | 1 | 1699.372077 | 1 | Female |
4 | 5 | 1 | 1633.978666 | 1 | Male |
Swap places on first ("Accuracy) and last column ("Sub_id"). We start creating a list of the column names and swap the first item to the last:
columns = data_frame.columns.tolist()
columns = columns[-1:] + columns[:-1]
We continue with using the created list and the columns in the data frame will change places.
data_frame = data_frame[columns]
data_frame.head()
Gender | Sub_id | Condition | RT | Accuracy | |
---|---|---|---|---|---|
0 | Male | 1 | 1 | 1692.560014 | 0 |
1 | Female | 2 | 1 | 1565.725442 | 1 |
2 | Male | 3 | 1 | 1829.502355 | 1 |
3 | Female | 4 | 1 | 1699.372077 | 1 |
4 | Male | 5 | 1 | 1633.978666 | 1 |
Sorting the data frame ascending again (i.e., alphabetical):
data_frame = data_frame.sort_index(axis=1 ,ascending=True)
data_frame.head()
Accuracy | Condition | Gender | RT | Sub_id | |
---|---|---|---|---|---|
0 | 0 | 1 | Male | 1692.560014 | 1 |
1 | 1 | 1 | Female | 1565.725442 | 2 |
2 | 1 | 1 | Male | 1829.502355 | 3 |
3 | 1 | 1 | Female | 1699.372077 | 4 |
4 | 1 | 1 | Male | 1633.978666 | 5 |
Complete reversal of data frame. The second line reverses the list. Third, the data frame is reversed using that list.
columns = data_frame.columns.tolist()
columns = columns[::-1]
data_frame = data_frame[columns]
data_frame.head()
Sub_id | RT | Gender | Condition | Accuracy | |
---|---|---|---|---|---|
0 | 1 | 1692.560014 | Male | 1 | 0 |
1 | 2 | 1565.725442 | Female | 1 | 1 |
2 | 3 | 1829.502355 | Male | 1 | 1 |
3 | 4 | 1699.372077 | Female | 1 | 1 |
4 | 5 | 1633.978666 | Male | 1 | 1 |
data_frame["Sub_id"] = data_frame["Sub_id"].values[::-1]
data_frame.head()
Sub_id | RT | Gender | Condition | Accuracy | |
---|---|---|---|---|---|
0 | 30 | 1692.560014 | Male | 1 | 0 |
1 | 29 | 1565.725442 | Female | 1 | 1 |
2 | 28 | 1829.502355 | Male | 1 | 1 |
3 | 27 | 1699.372077 | Female | 1 | 1 |
4 | 26 | 1633.978666 | Male | 1 | 1 |
If we want to reverse the order of the rows. We start by re-order the data frame ascending:
data_frame = data_frame.sort_index(axis=1 ,ascending=True)
data_frame = data_frame.iloc[::-1]
data_frame.head()
Accuracy | Condition | Gender | RT | Sub_id | |
---|---|---|---|---|---|
29 | 1 | 0 | Female | 1192.754529 | 1 |
28 | 1 | 0 | Male | 1059.075141 | 2 |
27 | 1 | 0 | Female | 1066.748249 | 3 |
26 | 1 | 0 | Male | 1114.864145 | 4 |
25 | 1 | 0 | Female | 1117.844911 | 5 |
data_frame = data_frame.sort_index(ascending=True, axis=0)
data_frame.head()
Accuracy | Condition | Gender | RT | Sub_id | |
---|---|---|---|---|---|
0 | 0 | 1 | Male | 1692.560014 | 30 |
1 | 1 | 1 | Female | 1565.725442 | 29 |
2 | 1 | 1 | Male | 1829.502355 | 28 |
3 | 1 | 1 | Female | 1699.372077 | 27 |
4 | 1 | 1 | Male | 1633.978666 | 26 |
data_frame = data_frame.reindex(index=data_frame.index[::-1])
data_frame.head()
Accuracy | Condition | Gender | RT | Sub_id | |
---|---|---|---|---|---|
29 | 1 | 0 | Female | 1192.754529 | 1 |
28 | 1 | 0 | Male | 1059.075141 | 2 |
27 | 1 | 0 | Female | 1066.748249 | 3 |
26 | 1 | 0 | Male | 1114.864145 | 4 |
25 | 1 | 0 | Female | 1117.844911 | 5 |