如何为特定work_item的不同阶段设置时差

时间:2019-02-25 07:08:10

标签: python python-3.x pandas

在下图中,我想为每个阶段的特定work_item设置时差

for (int i = 1; i<=5;i++) {
   int sum = 0; // reset sum before each iteration
   for (int j =1;j<=i;j++) {
       sum = sum + j; // sum all numbers
       System.out.print(j + " "); // print each number
   }
   System.out.print("|  " +sum); // print the sum, with a padding, to separate results
   System.out.println(); // add a new line
}

1 个答案:

答案 0 :(得分:1)

解决方案始终在下一行中按行from_phaseto_phase每组开始。

首先转换列to_datetime,然后通过DataFrameGroupBy.diff为每个组的差异创建新列。

然后按dropna删除每组第NaN个行,汇总sum,将时间增量转换为total_seconds,最后添加reset_index

df['timestamp'] = pd.to_datetime(df['timestamp'])
df['diff'] = df.groupby('work_item')['timestamp'].diff()

df = (df.dropna(subset=['diff'])
        .groupby(['work_item','from_phase'])['diff']
        .sum()
        .dt.total_seconds()
        .astype(int)
        .reset_index(name='sum of differencies'))
print (df)

   work_item from_phase  sum of differencies
0  WI_000001    Analyze               416280
1  WI_000001     Deploy              1729920
2  WI_000001     Design                 2820
3  WI_000002    Analyze              1017720
4  WI_000002      Build                    0
5  WI_000002     Design                    0