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()
1#old df columns
2df.columns
3Index(['A', 'B', 'C', 'D'],dtype='***')
4#new column format that we want to rearange
5new_col = ['D','C','B','A'] #list of column name in order that we want
6
7df = df[new_col]
8df.columns
9Index(['D', 'C', 'B', 'A'],dtype='***')
10#new column order
1cols = df.columns.tolist()
2# Rearrange the list any way you want
3cols = cols[-1:] + cols[:-1]
4df = df[cols]