How to change the order of DataFrame columns? How to change the order of DataFrame columns? python python

How to change the order of DataFrame columns?


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]: dfOut[6]:          0         1         2         3         4      mean0  0.445598  0.173835  0.343415  0.682252  0.582616  0.4455431  0.881592  0.696942  0.702232  0.696724  0.373551  0.6702082  0.662527  0.955193  0.131016  0.609548  0.804694  0.6325963  0.260919  0.783467  0.593433  0.033426  0.512019  0.4366534  0.131842  0.799367  0.182828  0.683330  0.019485  0.3633715  0.498784  0.873495  0.383811  0.699289  0.480447  0.5871656  0.388771  0.395757  0.745237  0.628406  0.784473  0.5885297  0.147986  0.459451  0.310961  0.706435  0.100914  0.3451498  0.394947  0.863494  0.585030  0.565944  0.356561  0.5531959  0.689260  0.865243  0.136481  0.386582  0.730399  0.561593In [7]: cols = df.columns.tolist()In [8]: colsOut[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]: colsOut[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]: dfOut[17]:       mean         0         1         2         3         40  0.445543  0.445598  0.173835  0.343415  0.682252  0.5826161  0.670208  0.881592  0.696942  0.702232  0.696724  0.3735512  0.632596  0.662527  0.955193  0.131016  0.609548  0.8046943  0.436653  0.260919  0.783467  0.593433  0.033426  0.5120194  0.363371  0.131842  0.799367  0.182828  0.683330  0.0194855  0.587165  0.498784  0.873495  0.383811  0.699289  0.4804476  0.588529  0.388771  0.395757  0.745237  0.628406  0.7844737  0.345149  0.147986  0.459451  0.310961  0.706435  0.1009148  0.553195  0.394947  0.863494  0.585030  0.565944  0.3565619  0.561593  0.689260  0.865243  0.136481  0.386582  0.730399


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


Just assign the column names in the order you want them:

In [39]: dfOut[39]:           0         1         2         3         4  mean0  0.172742  0.915661  0.043387  0.712833  0.190717     11  0.128186  0.424771  0.590779  0.771080  0.617472     12  0.125709  0.085894  0.989798  0.829491  0.155563     13  0.742578  0.104061  0.299708  0.616751  0.951802     14  0.721118  0.528156  0.421360  0.105886  0.322311     15  0.900878  0.082047  0.224656  0.195162  0.736652     16  0.897832  0.558108  0.318016  0.586563  0.507564     17  0.027178  0.375183  0.930248  0.921786  0.337060     18  0.763028  0.182905  0.931756  0.110675  0.423398     19  0.848996  0.310562  0.140873  0.304561  0.417808     1In [40]: df = df[['mean', 4,3,2,1]]

Now, 'mean' column comes out in the front:

In [41]: dfOut[41]:    mean         4         3         2         10     1  0.190717  0.712833  0.043387  0.9156611     1  0.617472  0.771080  0.590779  0.4247712     1  0.155563  0.829491  0.989798  0.0858943     1  0.951802  0.616751  0.299708  0.1040614     1  0.322311  0.105886  0.421360  0.5281565     1  0.736652  0.195162  0.224656  0.0820476     1  0.507564  0.586563  0.318016  0.5581087     1  0.337060  0.921786  0.930248  0.3751838     1  0.423398  0.110675  0.931756  0.1829059     1  0.417808  0.304561  0.140873  0.310562


matomo