我有以下DataFrame(df):

import numpy as np
import pandas as pd

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

我通过分配添加更多列:

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

如何将列的意思移到前面,即将其设置为第一列,而其他列的顺序保持不变?


当前回答

这里有一个非常简单的答案(只有一行)。

在将“n”列添加到df中之后,可以执行以下操作。

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.rand(10, 5))
df['mean'] = df.mean(1)
df
           0           1           2           3           4        mean
0   0.929616    0.316376    0.183919    0.204560    0.567725    0.440439
1   0.595545    0.964515    0.653177    0.748907    0.653570    0.723143
2   0.747715    0.961307    0.008388    0.106444    0.298704    0.424512
3   0.656411    0.809813    0.872176    0.964648    0.723685    0.805347
4   0.642475    0.717454    0.467599    0.325585    0.439645    0.518551
5   0.729689    0.994015    0.676874    0.790823    0.170914    0.672463
6   0.026849    0.800370    0.903723    0.024676    0.491747    0.449473
7   0.526255    0.596366    0.051958    0.895090    0.728266    0.559587
8   0.818350    0.500223    0.810189    0.095969    0.218950    0.488736
9   0.258719    0.468106    0.459373    0.709510    0.178053    0.414752


### here you can add below line and it should work 
# Don't forget the two (()) 'brackets' around columns names.Otherwise, it'll give you an error.

df = df[list(('mean',0, 1, 2,3,4))]
df

        mean           0           1           2           3           4
0   0.440439    0.929616    0.316376    0.183919    0.204560    0.567725
1   0.723143    0.595545    0.964515    0.653177    0.748907    0.653570
2   0.424512    0.747715    0.961307    0.008388    0.106444    0.298704
3   0.805347    0.656411    0.809813    0.872176    0.964648    0.723685
4   0.518551    0.642475    0.717454    0.467599    0.325585    0.439645
5   0.672463    0.729689    0.994015    0.676874    0.790823    0.170914
6   0.449473    0.026849    0.800370    0.903723    0.024676    0.491747
7   0.559587    0.526255    0.596366    0.051958    0.895090    0.728266
8   0.488736    0.818350    0.500223    0.810189    0.095969    0.218950
9   0.414752    0.258719    0.468106    0.459373    0.709510    0.178053

其他回答

此函数避免了您只需列出数据集中的每个变量来对其中的几个变量进行排序。

def order(frame,var):
    if type(var) is str:
        var = [var] #let the command take a string or list
    varlist =[w for w in frame.columns if w not in var]
    frame = frame[var+varlist]
    return frame 

它需要两个参数,第一个是数据集,第二个是要放到前面的数据集中的列。

所以在我的例子中,我有一个名为Frame的数据集,其中包含变量A1、A2、B1、B2、Total和Date。如果我想把道达尔带到前面,那么我所要做的就是:

frame = order(frame,['Total'])

如果我想将Total和Date带到前台,那么我会:

frame = order(frame,['Total','Date'])

编辑:

另一种有用的使用方法是,如果您有一个不熟悉的表,并且正在查找其中包含特定术语的变量,例如VAR1、VAR2,。。。您可以执行以下操作:

frame = order(frame,[v for v in frame.columns if "VAR" in v])

另一种选择是使用set_index()方法,后跟reset_index()。请注意,我们首先pop()将要移动到数据帧前面的列,以便在重置索引时避免名称冲突:

df.set_index(df.pop('column_name'), inplace=True)
df.reset_index(inplace=True)

有关详细信息,请参阅How to change the order of dataframe columns in panda。

您可以执行以下操作(从Aman的答案中借用零件):

cols = df.columns.tolist()
cols.insert(0, cols.pop(-1))

cols
>>>['mean', 0L, 1L, 2L, 3L, 4L]

df = df[cols]

你也可以这样做:

df = df[['mean', '0', '1', '2', '3']]

您可以通过以下方式获取列列表:

cols = list(df.columns.values)

输出将产生:

['0', '1', '2', '3', 'mean']

…然后,在将其放入第一个函数之前,可以手动重新排列

这个问题以前已经回答过,但reindex_axis现在已被弃用,因此我建议使用:

df = df.reindex(sorted(df.columns), axis=1)

对于那些想要指定他们想要的顺序而不是仅仅对它们进行排序的人来说,下面列出了解决方案:

df = df.reindex(['the','order','you','want'], axis=1)

现在,如何对列名列表排序真的不是熊猫问题,而是Python列表操作问题。有很多方法可以做到这一点,我认为这个答案有一个非常简洁的方法。