Python:Groupby的格式化问题

时间:2018-07-26 10:59:50

标签: python dataframe

我正在尝试按薪水栏分组以获取每种薪水类别的出现频率,如下所示:

private static class ArraySlice<T> extends AbstractList<T> {
    private final T[] data;
    private final int start;
    private final int length;

    public ArraySlice(T[] data, int start, int length) {
        this.data = data;
        this.start = start;
        this.length = length;
    }

    @Override
    public T get(int index) {
        if(index > length) throw new IndexOutOfBoundsException("Out of bounds");
        return data[start + index];
    }

    @Override
    public int size() {
        return length;
    }
}

public static <T> List<List<T>> matrixize(int cutat, T... data) {
    List<List<T>> matrix = new ArrayList<>();
    for(int i = 0; i < data.length; i += cutat) {
        matrix.add(new ArraySlice(data, i, Math.min(cutat, data.length - i)));
    }
    return matrix;
}

public void test() {
    List<List<Integer>> matrix = matrixize(2, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    System.out.println(matrix);
}

但是它给出了以下输出,其中SAL和FREQUENCY标头未正确对齐:

def PROC_FREQ(dataset,arg1):

        x= dataset.groupby(arg1)[arg1[-1]].agg(({'Frequency':'count'}))


        print('\n')
        print(x)
        print('\n')

PROC_FREQ(df,['SAL'])

我想要如下输出,以便SAL和FREQUENCY标头应正确对齐:

     Frequency
SAL           
100          2
200          1
300          1

请帮助。

1 个答案:

答案 0 :(得分:0)

请使用reset_index

您的df看起来像这样,

       Frequency
SAL        
100       2
200       1
300       1


df=df.reset_index()
print(df)        
SAL     Frequency      
100          2
200          1
300          1