pandas 数据透视表 - 更改非索引列的顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36346071/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:58:28  来源:igfitidea点击:

pandas pivot table - changing order of non-index columns

pythonpandas

提问by Arbitrage84

I created a pivot table using:

我使用以下方法创建了一个数据透视表:

table2 = pandas.pivot_table(df, index=['Salesperson'], values=['Gross Sales', 'Gross Profit'], aggfunc=numpy.sum)
table2['Profit Margin'] = table2['Gross Profit'] / table2['Gross Sales']
table2_rounded = table2.round({'Gross Profit': 2, 'Gross Sales': 2, 'Profit Margin': 2})

which gives me:

这给了我:

in: table2.info
out: Salesperson Gross Profit Gross Sales Profit Margin
  ((((values as row data))))

as columns. HOWEVER - Gross Sales should display before Gross Profit. How do I change the order of the non-index columns? The dataframe was 1000 rows long before I pivoted. I searched high and low for a solution. This seems rather basic (or should be...)

作为列。但是 - 总销售额应显示在总利润之前。如何更改非索引列的顺序?在我旋转之前,数据框是 1000 行。我四处寻找解决方案。这似乎相当基本(或应该是......)

回答by Haleemur Ali

You can reindex the axis in the order you want. The appropriate method is called reindex_axis.

您可以按照您想要的顺序重新索引轴。调用适当的方法reindex_axis

_note: reindex_axis is deprecated since version 0.21.0: Use reindex instead._

_注意:reindex_axis 自 0.21.0 版起已弃用:改用 reindex。_

column_order = ['Gross Sales', 'Gross Profit', 'Profit Margin']
# before pandas 0.21.0
table3 = table2.reindex_axis(column_order, axis=1)
# after pandas 0.21.0
table3 = table2.reindex(column_order, axis=1)

The method infois not meant to display the DataFrame, and it is not being called correctly. To call info, try typing in table2.info()instead. It is possible to examine the DataFrame by just typing the variable name, calling the print function [or statement], using the headand tailmethods, or slicing a row / column range.

该方法info不是为了显示 DataFrame,它没有被正确调用。要调用info,请尝试输入table2.info()。只需键入变量名称、调用打印函数 [或语句]、使用headandtail方法或切片行/列范围,就可以检查 DataFrame 。

回答by Acccumulation

You can re-order columns by taking a slice of the data frame:

您可以通过获取数据框的一部分来重新排序列:

table3 = table2[['Gross Sales', 'Gross Profit', 'Profit Margin']].copy()

table3 = table2[['Gross Sales', 'Gross Profit', 'Profit Margin']].copy()

Note that I have a set of brackets for the slice, and another set of brackets to enclose the list of column names. If you do table2['Gross Sales', 'Gross Profit', 'Profit Margin'], it will throw an error. Also, since this is taking a slice, omitting .copy()will result in a shallow copy.

请注意,我有一组用于切片的括号,还有一组用于包含列名称列表的括号。如果你这样做 table2['Gross Sales', 'Gross Profit', 'Profit Margin'],它会抛出一个错误。此外,由于这是一个切片,省略.copy()将导致浅拷贝。

I don't know of any benefits of using reindex_axisif you aren't using the optional parameters, so anyone who knows of such, feel free to mention in the comments.

reindex_axis如果您不使用可选参数,我不知道使用有什么好处,所以任何知道这样的人,请随时在评论中提及。

And if you're using Spyder, you can view the dataframe by going to the variable explorer and clicking on its name.

如果您使用的是 Spyder,则可以通过转到变量资源管理器并单击其名称来查看数据框。