【问题标题】:Rearranging order in Pivot Table在数据透视表中重新排列顺序
【发布时间】:2016-08-26 03:42:15
【问题描述】:

我在 pandas 中有以下数据透视表:

   Division             BU/CF        Allocation Key CurrentHC 

0  Central Functions     A            NEF           3 
1                        B            NEF           2 
2                        C            EXP           1 
3                                     NEF           4 
4                        D            NEF           3 
5  Xerxes                E            NLE           4 
6                        F            NLE           1 
7                        G            NLE           1 
8                        H            NLE           5 

Python 显然按字母顺序对除法和 BU/CF 进行排序。如何将自己的订单应用到数据透视表。

期望的输出:

   Division              BU/CF       Allocation Key CurrentHC 
0  Central Functions     D            NEF           3 
1                        B            NEF           2 
2                        C            EXP           1 
3                                     NEF           4 
4                        A            NEF           3 
5  Xerxes                E            NLE           4 
6                        H            NLE           5 
7                        G            NLE           1 
8                        F            NLE           1 

我用来创建数据透视表的代码:

#Create full report pivot 
report_pivot = pd.pivot_table(full_report, index=["Division","BU/CF", "Allocation Key"], 
                       values=["Previous HC", "New Hire", "Resigned", "In", "Out", "Current HC", "Delta"], 

                       fill_value=0) 

我设法通过这样做重新排列了列:

# Reorderr columns 
cols = [ "Previous HC", "New Hire", "Resigned", "In", "Out","Delta", "Current HC"] 
report_pivot = report_pivot[cols] 

索引有没有类似的方法。特别是“BU/CF”

*我排除了除 Current HC 之外的其他列以简化上表

【问题讨论】:

  • 也许有帮助:stackoverflow.com/questions/10595327/pandas-sort-pivot-table 请提供完整的代码和数据,以便我们轻松调整解决方案。
  • 我添加了用于制作数据透视表的代码
  • 查看您想要的 DF 绝对不清楚 my own order 的含义。你能定义排序标准吗?
  • 好的,对于例如“BU/CF”中的薛西斯除法,顺序是按字母顺序排列的:[E, F, G, H]。我希望订单是这样的:[E,H,G,F]。如何排序没有标准,我想要的顺序由我决定。我希望我现在可以说清楚

标签: python pandas pivot-table


【解决方案1】:

你可以这样做:

In [62]: sort_map = {
   ....:  'E': 10,
   ....:  'H': 20,
   ....:  'G': 30,
   ....:  'F': 40,
   ....: }

In [63]: df.loc[df['Division'] == 'Xerxes', 'BU/CF'].map(sort_map)
Out[63]:
5    10
6    40
7    30
8    20
Name: BU/CF, dtype: int64

In [64]: idx = df.loc[df['Division'] == 'Xerxes', 'BU/CF'].map(sort_map).sort_values().index

In [65]: idx
Out[65]: Int64Index([5, 8, 7, 6], dtype='int64')

In [66]: df[df['Division'] == 'Xerxes'].reindex(idx)
Out[66]:
  Division BU/CF AllocationKey  CurrentHC
5   Xerxes     E           NLE          4
8   Xerxes     H           NLE          5
7   Xerxes     G           NLE          1
6   Xerxes     F           NLE          1

更新:

从 Pandas 0.20.1 the .ix indexer is deprecated, in favor of the more strict .iloc and .loc indexers 开始。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2016-04-10
    • 2022-01-13
    • 2020-04-17
    • 2011-12-06
    • 2015-03-20
    • 2021-10-27
    相关资源
    最近更新 更多