Python 在 Pandas 数据框中设置列的顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41968732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set order of columns in pandas dataframe
提问by durbachit
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.)
(请提供一个通用的解决方案,而不是针对这种情况。非常感谢。)
回答by A.Kot
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.]]
回答by Okroshiashvili
You can use this:
你可以使用这个:
columnsTitles = ['onething', 'secondthing', 'otherthing']
frame = frame.reindex(columns=columnsTitles)
回答by Lala La
Here is a solution I use very often. When you have a large data set with tons of columns, you definitely do not want to manually rearrange all the columns.
这是我经常使用的解决方案。当您拥有包含大量列的大型数据集时,您绝对不想手动重新排列所有列。
What you can and, most likely, want to do is to just order the first a few columns that you frequently use, and let all other columns just be themselves. This is a common approach in R. df %>%select(one, two, three, everything())
您可以而且很可能想要做的是只订购您经常使用的前几列,而让所有其他列成为它们自己。这是 R 中的常用方法。df %>%select(one, two, three, everything())
So you can first manually type the columns that you want to order and to be positioned before all the other columns in a list cols_to_order
.
因此,您可以先手动键入要排序的列,并将其置于列表中所有其他列之前cols_to_order
。
Then you construct a list for new columns by combining the rest of the columns:
然后通过组合其余列来构建新列的列表:
new_columns = cols_to_order + (frame.columns.drop(cols_to_order).tolist())
After this, you can use the new_columns
as other solutions suggested.
在此之后,您可以使用new_columns
建议的其他解决方案。
import pandas as pd
frame = pd.DataFrame({
'one thing': [1, 2, 3, 4],
'other thing': ['a', 'e', 'i', 'o'],
'more things': ['a', 'e', 'i', 'o'],
'second thing': [0.1, 0.2, 1, 2],
})
cols_to_order = ['one thing', 'second thing']
new_columns = cols_to_order + (frame.columns.drop(cols_to_order).tolist())
frame = frame[new_columns]
one thing second thing other thing more things
0 1 0.1 a a
1 2 0.2 e e
2 3 1.0 i i
3 4 2.0 o o
回答by omri_saadon
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.
然后很容易手动重新排列。
回答by piRSquared
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
回答by MaxU
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
回答by irene
Add the 'columns' parameter:
添加“列”参数:
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']
)
回答by U10-Forward
Try indexing (so you want a generic solution not only for this, so index order can be just what you want):
尝试索引(因此您不仅需要通用解决方案,因此索引顺序可以是您想要的):
l=[0,2,1] # index order
frame=frame[[frame.columns[i] for i in l]]
Now:
现在:
print(frame)
Is:
是:
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
回答by Sando K
I find this to be the most straightforward and working:
我发现这是最直接和最有效的:
df = pd.DataFrame({
'one thing':[1,2,3,4],
'second thing':[0.1,0.2,1,2],
'other thing':['a','e','i','o']})
df = df[['one thing','second thing', 'other thing']]