在数据透视表中重新排列顺序

时间:2016-05-01 07:58:38

标签: python pandas pivot-table

我在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"

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

1 个答案:

答案 0 :(得分:1)

你可以做这样的事情:

#include <stdlib.h>
#include <stdio.h>

char* reverseString(char*);

int main(void)
{
    char* s = "hello";
    char* out;

    out = reverseString(s);

    printf("%p %s\n", out, out);
    return 0;
}

char* reverseString(char* s)
{
    int i;
    int size;
    char* reverse;

    size = sizeof(s);
    char reversechar[size];

    for (i = 1; i < size + 1; i++)
        reversechar[i - 1] = s[size - i];

    reverse = reversechar;
    //printf("%p\n",reverse);
    return reverse;
}

<强>更新

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