熊猫-获取分组数据的最新'n'列值

时间:2018-10-29 10:57:35

标签: python pandas

我是大熊猫,并且有以下工作计划程序数据:

| Job Name   | Region        | Status | Timestamp            |
| some_job_1 | some_region_1 | DONE   | 2018-10-02T03:46:25Z |
| some_job_1 | some_region_2 | ERROR  | 2018-10-02T03:44:25Z |
| some_job_2 | some_region_1 | DONE   | 2018-10-01T03:46:25Z |
| some_job_1 | some_region_2 | ERROR  | 2018-11-02T03:44:25Z |

现在,我想要一个时间范围内前5个失败的作业,它是最后一个'n'执行状态。看起来应该像这样:

| Job Name   | Region        | DONE | ERROR | Last 5 runs                  |
| some_job_1 | some_region_1 | 3    | 12    | ERROR DONE ERROR ERROR ERROR |
| some_job_1 | some_region_2 | 2    | 9     | ERROR DONE ERROR ERROR ERROR |
| some_job_2 | some_region_1 | 2    | 8     | ERROR DONE ERROR ERROR ERROR |
| some_job_2 | some_region_2 | 5    | 7     | ERROR DONE ERROR ERROR ERROR |
| some_job_3 | some_region_2 | 5    | 7     | ERROR DONE ERROR ERROR ERROR |

我已经做到了这一点:

| Job Name   | Region        | DONE | ERROR | 
| some_job_1 | some_region_1 | 3    | 12    | 
| some_job_1 | some_region_2 | 2    | 9     | 
| some_job_2 | some_region_1 | 2    | 8     | 
| some_job_2 | some_region_2 | 5    | 7     | 
| some_job_3 | some_region_2 | 5    | 7     | 

使用:

data.groupby(['Job Name', 'Region']).Status.value_counts().unstack().fillna(0).sort_values('ERROR', ascending=False).head(5)

我尝试使用last(),但没有成功。感谢有人可以帮助我!

1 个答案:

答案 0 :(得分:1)

您可以在单独的agg函数中定义聚合并将其应用于groupby对象。

public class Project1 {

public static int[] shiftone(int[]n,boolean left) {
    n = new int[n.length];


    int save,save2;
    if(left = true){
        save = n[0];
        save2 = n[(n.length-1)];
        for (int i = 1; i < n.length-1; i++) {
            n[i-1]=n[i];
        }
        n[n.length-1] = save;
        n[n.length-2] = save2;
    }
    else{
        save = n[n.length-1];
        for (int i=0;i<(n.length-1);i++)
            n[(n.length)-i] = n[(n.length-1)-1];
       n[0] = save; 
    }
    return n;
}

public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    int[] x;
    int k;

    boolean left;
    System.out.print("Masukkan jumlah data yang ingin diinput: ");
    k = input.nextInt();
    System.out.println();
    x = new int[k];

    for (int i = 0; i < k; i++) {
    System.out.print("Input data ke-"+i+": ");
    x[i] = input.nextInt(); 
    }
    System.out.print("Array: "+Arrays.toString(x));
    System.out.println();

    System.out.print("Move to left? (true/false): ");
    left = input.nextBoolean();
    System.out.println();

    int[] y;
    y = new int[k];
    y = shiftone(x,left);
    System.out.print("New array: "+Arrays.toString(y));
    }
}