How to change the order of DataFrame columns?
How to Change the order of DataFrame columns?
One of the easy way would be to reassign the data frame with a list of the columns, rearranged as required.
This is what you have now:
In [6]: df Out[6]: 0 1 2 3 4 mean 0 0.445598 0.173835 0.343415 0.682252 0.582616 0.445543 1 0.881592 0.696942 0.702232 0.696724 0.373551 0.670208 2 0.662527 0.955193 0.131016 0.609548 0.804694 0.632596 3 0.260919 0.783467 0.593433 0.033426 0.512019 0.436653 4 0.131842 0.799367 0.182828 0.683330 0.019485 0.363371 5 0.498784 0.873495 0.383811 0.699289 0.480447 0.587165 6 0.388771 0.395757 0.745237 0.628406 0.784473 0.588529 7 0.147986 0.459451 0.310961 0.706435 0.100914 0.345149 8 0.394947 0.863494 0.585030 0.565944 0.356561 0.553195 9 0.689260 0.865243 0.136481 0.386582 0.730399 0.561593 In [7]: cols = df.columns.tolist() In [8]: cols Out[8]: [0L, 1L, 2L, 3L, 4L, 'mean']
Rearrange cols in any way you want. This is how I moved the last element to the first position:
In [12]: cols = cols[-1:] + cols[:-1] In [13]: cols Out[13]: ['mean', 0L, 1L, 2L, 3L, 4L]
Then reorder the dataframe like this:
In [16]: df = df[cols] # OR df = df.ix[:, cols] In [17]: df Out[17]: mean 0 1 2 3 4 0 0.445543 0.445598 0.173835 0.343415 0.682252 0.582616 1 0.670208 0.881592 0.696942 0.702232 0.696724 0.373551 2 0.632596 0.662527 0.955193 0.131016 0.609548 0.804694 3 0.436653 0.260919 0.783467 0.593433 0.033426 0.512019 4 0.363371 0.131842 0.799367 0.182828 0.683330 0.019485 5 0.587165 0.498784 0.873495 0.383811 0.699289 0.480447 6 0.588529 0.388771 0.395757 0.745237 0.628406 0.784473 7 0.345149 0.147986 0.459451 0.310961 0.706435 0.100914 8 0.553195 0.394947 0.863494 0.585030 0.565944 0.356561 9 0.561593 0.689260 0.865243 0.136481 0.386582 0.730399
Given code is
import pandas as pd df = pd.DataFrame({'Age': [30, 20, 22, 40, 32, 28, 39], 'Color': ['Blue', 'Green', 'Red', 'White', 'Gray', 'Black', 'Red'], 'Food': ['Steak', 'Lamb', 'Mango', 'Apple', 'Cheese', 'Melon', 'Beans'], 'Height': [165, 70, 120, 80, 180, 172, 150], 'Score': [4.6, 8.3, 9.0, 3.3, 1.8, 9.5, 2.2], 'State': ['NY', 'TX', 'FL', 'AL', 'AK', 'TX', 'TX'] }, index=['Jane', 'Nick', 'Aaron', 'Penelope', 'Dean', 'Christina', 'Cornelia']) print("\n -- Change order using columns -- \n") new_order = [3, 2, 1, 4, 5, 0] df = df[df.columns[new_order]] print(df) print("\n -- Change order using reindex -- \n") df = df.reindex(['State', 'Color', 'Age', 'Food', 'Score', 'Height'], axis=1) print(df)
As we can rearrange a DataFrame object by declaring a list of columns and using it as a key.
import pandas as pd fruit = pd.DataFrame(data = {'Fruit':['apple', 'banana', 'blueberry', 'grape'], 'Color':['red', 'yellow', 'blue', 'purple'], 'Seeds':['yes', 'no', 'yes', 'no']}) fruit.set_index('Fruit', inplace = True)
Here, the DataFrame is structured like so:
>>> fruit Color Seeds Fruit apple red yes banana yellow no blueberry blue yes grape purple no
def change_column_order(df, col_name, index):
cols = df.columns.tolist()
cols.remove(col_name)
cols.insert(index, col_name)
return df[cols]
in this case, this would be like:
df = change_column_order(df, 'mean', 0)
You could also do something like this:
df = df[[‘mean’, ‘0’, ‘1’, ‘2’, ‘3’]]
You can get the list of columns with:
cols = list(df.columns.values)
The output will produce:
[‘0’, ‘1’, ‘2’, ‘3’, ‘mean’]
…which is then easy to rearrange manually before dropping it into the first function
You need to create a new list of your columns in the desired order, then use df = df[cols] to rearrange the columns in this new order.
cols = [‘mean’] + [col for col in df if col != ‘mean’]
df = df[cols]
You can also use a more general approach. In this example, the last column (indicated by -1) is inserted as the first column.
cols = [df.columns[-1]] + [col for col in df if col != df.columns[-1]]
df = df[cols]
You can also use this approach for reordering columns in a desired order if they are present in the DataFrame.
inserted_cols = [‘a’, ‘b’, ‘c’]
cols = ([col for col in inserted_cols if col in df]
+ [col for col in df if col not in inserted cols])
df = df[cols]
One easy way would be to reassign the dataframe with a list of the columns, rearranged as needed.
This is what you have now:
In [6]: df
Out[6]:
0 1 2 3 4 mean
0 0.445598 0.173835 0.343415 0.682252 0.582616 0.445543
1 0.881592 0.696942 0.702232 0.696724 0.373551 0.670208
2 0.662527 0.955193 0.131016 0.609548 0.804694 0.632596
3 0.260919 0.783467 0.593433 0.033426 0.512019 0.436653
4 0.131842 0.799367 0.182828 0.683330 0.019485 0.363371
5 0.498784 0.873495 0.383811 0.699289 0.480447 0.587165
6 0.388771 0.395757 0.745237 0.628406 0.784473 0.588529
7 0.147986 0.459451 0.310961 0.706435 0.100914 0.345149
8 0.394947 0.863494 0.585030 0.565944 0.356561 0.553195
9 0.689260 0.865243 0.136481 0.386582 0.730399 0.561593
In [7]: cols = df.columns.tolist()
In [8]: cols
Out[8]: [0L, 1L, 2L, 3L, 4L, ‘mean’
new_order = [3,2,1,4,5,0]
df = df[df.columns[new_order]]
print(df)
a c b mean d e
0 0.637589 0.634264 0.733961 0.617316 0.534911 0.545856
1 0.854449 0.830046 0.883416 0.678389 0.183003 0.641032
2 0.332996 0.195891 0.879472 0.545261 0.447813 0.870135
3 0.902704 0.843252 0.348227 0.677614 0.635780 0.658107
4 0.422357 0.529151 0.619282 0.412559 0.405749 0.086255
5 0.251454 0.940245 0.068633 0.554269 0.691631 0.819380
6 0.423781 0.179961 0.643971 0.361245 0.105050 0.453460
7 0.680696 0.487651 0.255453 0.419046 0.330417 0.341014
8 0.276729 0.473765 0.981271 0.690007 0.817877 0.900394
9 0.964470 0.248088 0.609391 0.463661 0.128077 0.368279
And for the specific case of OP’s question:
new_order = [-1,0,1,2,3,4]
df = df[df.columns[new_order]]
print(df)
mean a b c d e
0 0.595177 0.329206 0.713246 0.712898 0.572263 0.648273
1 0.638860 0.452519 0.598171 0.797982 0.858137 0.487490
2 0.287636 0.100442 0.244445 0.288450 0.285795 0.519049
3 0.653974 0.863342 0.460811 0.782644 0.827890 0.335183
4 0.285233 0.004613 0.485135 0.014066 0.489957 0.432394
5 0.430761 0.630070 0.328865 0.528100 0.031827 0.634943
6 0.444338 0.102679 0.808613 0.389616 0.440022 0.480759
7 0.536163 0.063105 0.420832 0.959125 0.643879 0.593874
8 0.556107 0.716114 0.180603 0.668684 0.262900 0.952237
9 0.416280 0.816816 0.064956 0.178113 0.377693 0.643820