1# create a dataframe
2df = pd.DataFrame({'B':[1,2],'A':[0,0],'C':[1,1]})
3# reorder columns as ['A','B','C']
4df = df.reindex(columns = ['A','B','C'])
1cols = df.columns.tolist()
2cols = cols[-1:] + cols[:-1] #bring last element to 1st position
3df = df.reindex(cols, axis=1)
1# setting up a dummy dataframe
2raw_data = {'name': ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel'],
3 'age': [20, 19, 22, 21],
4 'favorite_color': ['blue', 'red', 'yellow', "green"],
5 'grade': [88, 92, 95, 70]}
6df = pd.DataFrame(raw_data, index = ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel'])
7df
8
9#now 'age' will appear at the end of our df
10df = df[['favorite_color','grade','name','age']]
11df.head()
1In [7]: cols = df.columns.tolist()
2In [8]: cols
3Out[8]: [0L, 1L, 2L, 3L, 4L, 'mean']
4
5In [12]: cols = cols[-1:] + cols[:-1]
6
7In [13]: cols
8Out[13]: ['mean', 0L, 1L, 2L, 3L, 4L]
9
10In [14]: df = df[cols]