How to change the order of DataFrame columns?

2018-12-31 19:39发布

I have the following DataFrame (df):

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.rand(10, 5))

I add more column(s) by assignment:

df['mean'] = df.mean(1)

How can I move the column mean to the front, i.e. set it as first column leaving the order of the other columns untouched?

标签: python pandas
25条回答
与风俱净
2楼-- · 2018-12-31 19:57

DataFrame.sort_index(axis=1) is quite clean.Check doc here. And then concat

查看更多
残风、尘缘若梦
3楼-- · 2018-12-31 19:58

From August 2018:

If your column names are too long to type then you could specify the new order through a list of integers with the positions:

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
查看更多
墨雨无痕
4楼-- · 2018-12-31 19:59

Just type the column name you want to change, and set the index for the new location.

def change_column_order(df, col_name, index):
    cols = df.columns.tolist()
    cols.remove(col_name)
    cols.insert(index, col_name)
    return df[cols]

For your case, this would be like:

df = change_column_order(df, 'mean', 0)
查看更多
泪湿衣
5楼-- · 2018-12-31 19:59

@clocker: Your solution was very helpful for me, as I wanted to bring two columns in front from a dataframe where I do not know exactly the names of all columns, because they are generated from a pivot statement before. So, if you are in the same situation: To bring columns in front that you know the name of and then let them follow by "all the other columns", I came up with the following general solution;

df = df.reindex_axis(['Col1','Col2'] + list(df.columns.drop(['Col1','Col2'])), axis=1)
查看更多
牵手、夕阳
6楼-- · 2018-12-31 20:02

I tried the insert() function as suggested by Wes McKinney.

df.insert(0, 'mean', df.mean(1))

This got the result that Timmie wanted, in one line, without the need to move that last column.

查看更多
琉璃瓶的回忆
7楼-- · 2018-12-31 20:03

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

In [39]: df
Out[39]: 
          0         1         2         3         4  mean
0  0.172742  0.915661  0.043387  0.712833  0.190717     1
1  0.128186  0.424771  0.590779  0.771080  0.617472     1
2  0.125709  0.085894  0.989798  0.829491  0.155563     1
3  0.742578  0.104061  0.299708  0.616751  0.951802     1
4  0.721118  0.528156  0.421360  0.105886  0.322311     1
5  0.900878  0.082047  0.224656  0.195162  0.736652     1
6  0.897832  0.558108  0.318016  0.586563  0.507564     1
7  0.027178  0.375183  0.930248  0.921786  0.337060     1
8  0.763028  0.182905  0.931756  0.110675  0.423398     1
9  0.848996  0.310562  0.140873  0.304561  0.417808     1

In [40]: df = df[['mean', 4,3,2,1]]

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

In [41]: df
Out[41]: 
   mean         4         3         2         1
0     1  0.190717  0.712833  0.043387  0.915661
1     1  0.617472  0.771080  0.590779  0.424771
2     1  0.155563  0.829491  0.989798  0.085894
3     1  0.951802  0.616751  0.299708  0.104061
4     1  0.322311  0.105886  0.421360  0.528156
5     1  0.736652  0.195162  0.224656  0.082047
6     1  0.507564  0.586563  0.318016  0.558108
7     1  0.337060  0.921786  0.930248  0.375183
8     1  0.423398  0.110675  0.931756  0.182905
9     1  0.417808  0.304561  0.140873  0.310562
查看更多
登录 后发表回答