Python 反转 DataFrame 列顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27817718/
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
Reverse DataFrame column order
提问by Boosted_d16
I want to simply reverse the column order of a given DataFrame.
我想简单地反转给定 DataFrame 的列顺序。
My DataFrame:
我的数据帧:
data = {'year': [2010, 2011, 2012, 2011, 2012, 2010, 2011, 2012],
'team': ['Bears', 'Bears', 'Bears', 'Packers', 'Packers', 'Lions', 'Lions', 'Lions'],
'wins': [11, 8, 10, 15, 11, 6, 10, 4],
'losses': [5, 8, 6, 1, 5, 10, 6, 12]}
football = pd.DataFrame(data, columns=['year', 'team', 'wins', 'losses'])
Actual output:
实际输出:
year team wins losses
0 2010 Bears 11 5
1 2011 Bears 8 8
2 2012 Bears 10 6
3 2011 Packers 15 1
4 2012 Packers 11 5
5 2010 Lions 6 10
6 2011 Lions 10 6
7 2012 Lions 4 12
I thought this would work but it reverses the row order not column order:
我认为这会起作用,但它反转了行顺序而不是列顺序:
football[::-1]
I also tried:
我也试过:
football.columns = football.columns[::-1]
but that reversed the column labels and not the entire column itself.
但这反转了列标签而不是整个列本身。
采纳答案by Alex Riley
A solution close to what you have already tried is to use:
接近您已经尝试过的解决方案是使用:
>>> football[football.columns[::-1]]
losses wins team year
0 5 11 Bears 2010
1 8 8 Bears 2011
2 6 10 Bears 2012
3 1 15 Packers 2011
4 5 11 Packers 2012
5 10 6 Lions 2010
6 6 10 Lions 2011
7 12 4 Lions 2012
football.columns[::-1]reverses the order of the DataFrame's sequence of columns, and football[...]reindexes the DataFrame using this new sequence.
football.columns[::-1]反转 DataFrame 的列序列的顺序,并football[...]使用这个新序列重新索引 DataFrame。
A more succinct way to achieve the same thing is with the ilocindexer:
实现相同目的的更简洁方法是使用iloc索引器:
football.iloc[:, ::-1]
The first :means "take all rows", the ::-1means step backwards through the columns.
第一个:意思是“获取所有行”,::-1意思是在列中后退。
The locindexer mentioned in @PietroBattiston's answer works in the same way.
loc@PietroBattiston 的回答中提到的索引器的工作方式相同。
回答by EdChum
Note: As of Pandas v0.20, .ixindexer is deprecatedin favour of .iloc/ .loc.
注意:从 Pandas v0.20 开始,.ix索引器已被弃用,取而代之的是.iloc/ .loc。
You can use fancy indexing .ix, pass the columns and then reverse the list to change the order:
您可以使用花式索引.ix,传递列,然后反转列表以更改顺序:
In [27]:
football.ix[::,football.columns[::-1]]
Out[27]:
losses wins team year
0 5 11 Bears 2010
1 8 8 Bears 2011
2 6 10 Bears 2012
3 1 15 Packers 2011
4 5 11 Packers 2012
5 10 6 Lions 2010
6 6 10 Lions 2011
7 12 4 Lions 2012
timings
时间
In [32]:
%timeit football[football.columns[::-1]]
1000 loops, best of 3: 421 μs per loop
In [33]:
%timeit football.ix[::,football.columns[::-1]]
1000 loops, best of 3: 403 μs per loop
fancy indexing is marginally faster in this case
在这种情况下,花式索引稍微快一点
回答by Pietro Battiston
Note: As of Pandas v0.20, .ixindexer is deprecatedin favour of .iloc/ .loc.
注意:从 Pandas v0.20 开始,.ix索引器已被弃用,取而代之的是.iloc/ .loc。
Close to EdChum's answer... but faster:
接近EdChum 的答案......但更快:
In [3]: %timeit football.ix[::,::-1]
1000 loops, best of 3: 255 μs per loop
In [4]: %timeit football.ix[::,football.columns[::-1]]
1000 loops, best of 3: 491 μs per loop
Also notice one colon is redundant:
还要注意一个冒号是多余的:
In [5]: all(football.ix[:,::-1] == football.ix[::,::-1])
Out[5]: True
EDIT:a further (minimal) improvement is brought by using .locrather than .ix, as in football.loc[:,::-1].
编辑:进一步(最小)改进是通过使用.loc而不是.ix,如football.loc[:,::-1]。

