设置pandas数据帧中的列顺序

时间:2021-10-29 04:21:32

Is there a way to reorder columns in pandas dataframe based on my personal preference (i.e. not alphabetically or numerically sorted, but more like following certain conventions)?

有没有办法根据我的个人偏好重新排序pandas数据框中的列(即不按字母顺序或数字排序,但更像是遵循某些约定)?

Simple example:

简单的例子:

frame = pd.DataFrame({
        'one thing':[1,2,3,4],
        'second thing':[0.1,0.2,1,2],
        'other thing':['a','e','i','o']})

produces this:

产生这个:

   one thing other thing  second thing
0          1           a           0.1
1          2           e           0.2
2          3           i           1.0
3          4           o           2.0

But instead, I would like this:

但相反,我想这样:

   one thing second thing  other thing
0          1           0.1           a
1          2           0.2           e
2          3           1.0           i
3          4           2.0           o

(Please, provide a generic solution rather than specific to this case. Many thanks.)

(请提供一个通用的解决方案,而不是针对这种情况。非常感谢。)

6 个解决方案

#1


63  

Just select the order yourself by typing in the column names. Note the double brackets:

只需输入列名称即可自行选择订单。注意双括号:

frame = frame[['column I want first', 'column I want second'...etc.]]

#2


17  

You could also do something like df = df[['x', 'y', 'a', 'b']]

你也可以做df = df [['x','y','a','b']]之类的事情

import pandas as pd
frame = pd.DataFrame({'one thing':[1,2,3,4],'second thing':[0.1,0.2,1,2],'other thing':['a','e','i','o']})
frame = frame[['second thing', 'other thing', 'one thing']]
print frame
   second thing other thing  one thing
0           0.1           a          1
1           0.2           e          2
2           1.0           i          3
3           2.0           o          4

Also, you can get the list of columns with:

此外,您可以获取列列表:

cols = list(df.columns.values)

The output will produce something like this:

输出将产生如下内容:

['x', 'y', 'a', 'b']

Which is then easy to rearrange manually.

这很容易手动重新排列。

#3


12  

You can use this:

你可以用这个:

columnsTitles = ['onething', 'secondthing', 'otherthing']

frame.reindex(columns=columnsTitles)

#4


8  

Construct it with a list instead of a dictionary

用列表而不是字典构造它

frame = pd.DataFrame([
        [1, .1, 'a'],
        [2, .2, 'e'],
        [3,  1, 'i'],
        [4,  4, 'o']
    ], columns=['one thing', 'second thing', 'other thing'])

frame

   one thing  second thing other thing
0          1           0.1           a
1          2           0.2           e
2          3           1.0           i
3          4           4.0           o

#5


6  

You can also use OrderedDict:

您也可以使用OrderedDict:

In [183]: from collections import OrderedDict

In [184]: data = OrderedDict()

In [185]: data['one thing'] = [1,2,3,4]

In [186]: data['second thing'] = [0.1,0.2,1,2]

In [187]: data['other thing'] = ['a','e','i','o']

In [188]: frame = pd.DataFrame(data)

In [189]: frame
Out[189]:
   one thing  second thing other thing
0          1           0.1           a
1          2           0.2           e
2          3           1.0           i
3          4           2.0           o

#6


3  

Add the 'columns' parameter:

添加'columns'参数:

frame = pd.DataFrame({
        'one thing':[1,2,3,4],
        'second thing':[0.1,0.2,1,2],
        'other thing':['a','e','i','o']},
        columns=['one thing', 'second thing', 'other thing']
)

#1


63  

Just select the order yourself by typing in the column names. Note the double brackets:

只需输入列名称即可自行选择订单。注意双括号:

frame = frame[['column I want first', 'column I want second'...etc.]]

#2


17  

You could also do something like df = df[['x', 'y', 'a', 'b']]

你也可以做df = df [['x','y','a','b']]之类的事情

import pandas as pd
frame = pd.DataFrame({'one thing':[1,2,3,4],'second thing':[0.1,0.2,1,2],'other thing':['a','e','i','o']})
frame = frame[['second thing', 'other thing', 'one thing']]
print frame
   second thing other thing  one thing
0           0.1           a          1
1           0.2           e          2
2           1.0           i          3
3           2.0           o          4

Also, you can get the list of columns with:

此外,您可以获取列列表:

cols = list(df.columns.values)

The output will produce something like this:

输出将产生如下内容:

['x', 'y', 'a', 'b']

Which is then easy to rearrange manually.

这很容易手动重新排列。

#3


12  

You can use this:

你可以用这个:

columnsTitles = ['onething', 'secondthing', 'otherthing']

frame.reindex(columns=columnsTitles)

#4


8  

Construct it with a list instead of a dictionary

用列表而不是字典构造它

frame = pd.DataFrame([
        [1, .1, 'a'],
        [2, .2, 'e'],
        [3,  1, 'i'],
        [4,  4, 'o']
    ], columns=['one thing', 'second thing', 'other thing'])

frame

   one thing  second thing other thing
0          1           0.1           a
1          2           0.2           e
2          3           1.0           i
3          4           4.0           o

#5


6  

You can also use OrderedDict:

您也可以使用OrderedDict:

In [183]: from collections import OrderedDict

In [184]: data = OrderedDict()

In [185]: data['one thing'] = [1,2,3,4]

In [186]: data['second thing'] = [0.1,0.2,1,2]

In [187]: data['other thing'] = ['a','e','i','o']

In [188]: frame = pd.DataFrame(data)

In [189]: frame
Out[189]:
   one thing  second thing other thing
0          1           0.1           a
1          2           0.2           e
2          3           1.0           i
3          4           2.0           o

#6


3  

Add the 'columns' parameter:

添加'columns'参数:

frame = pd.DataFrame({
        'one thing':[1,2,3,4],
        'second thing':[0.1,0.2,1,2],
        'other thing':['a','e','i','o']},
        columns=['one thing', 'second thing', 'other thing']
)