change order of headers pandas

Solutions on MaxInterview for change order of headers pandas by the best coders in the world

showing results for - "change order of headers pandas"
Ignacio
18 Mar 2017
1In [6]: df
2Out[6]:
3          0         1         2         3         4      mean
40  0.445598  0.173835  0.343415  0.682252  0.582616  0.445543
51  0.881592  0.696942  0.702232  0.696724  0.373551  0.670208
62  0.662527  0.955193  0.131016  0.609548  0.804694  0.632596
73  0.260919  0.783467  0.593433  0.033426  0.512019  0.436653
84  0.131842  0.799367  0.182828  0.683330  0.019485  0.363371
95  0.498784  0.873495  0.383811  0.699289  0.480447  0.587165
106  0.388771  0.395757  0.745237  0.628406  0.784473  0.588529
117  0.147986  0.459451  0.310961  0.706435  0.100914  0.345149
128  0.394947  0.863494  0.585030  0.565944  0.356561  0.553195
139  0.689260  0.865243  0.136481  0.386582  0.730399  0.561593
14
15In [7]: cols = df.columns.tolist()
16
17In [8]: cols
18  
19Out[8]: [0L, 1L, 2L, 3L, 4L, 'mean']
20         # Arrange the Cols
21      	 cols = ['mean', 0L, 1L, 2L, 3L, 4L]
22In [16]: df = df[cols]  #    OR    df = df.ix[:, cols]
23
24In [17]: df
25Out[17]:
26       mean         0         1         2         3         4
270  0.445543  0.445598  0.173835  0.343415  0.682252  0.582616
281  0.670208  0.881592  0.696942  0.702232  0.696724  0.373551
292  0.632596  0.662527  0.955193  0.131016  0.609548  0.804694
303  0.436653  0.260919  0.783467  0.593433  0.033426  0.512019
314  0.363371  0.131842  0.799367  0.182828  0.683330  0.019485
325  0.587165  0.498784  0.873495  0.383811  0.699289  0.480447
336  0.588529  0.388771  0.395757  0.745237  0.628406  0.784473
347  0.345149  0.147986  0.459451  0.310961  0.706435  0.100914
358  0.553195  0.394947  0.863494  0.585030  0.565944  0.356561
369  0.561593  0.689260  0.865243  0.136481  0.386582  0.730399