我有一个包含一些Jira问题的数据框,我试图按周和状态对它们进行排序,并获取每周每个状态的项目数量。因此,例如,我的数据框目前有两个星期是这样的:

2019-11-04     Authorize Work      4
               Await Work          1
               Check Work          4
               Closed              4
               Confirm Work        3
               Do Work             3
2019-11-11     Authorize Work      6
               Do Work             2


至此,我已经做到以下几点:

# Remove the time portion of the date
df['creation_date'] = df['creation_date'].dt.date
# Set the date to be a week long delta
df['creation_date'] = pd.to_datetime(df['creation_date']) - pd.to_timedelta(7, unit='d')
# Sort together by creation date within the week and the status, and do a count
endf = df.groupby([pd.Grouper(key="creation_date", freq="W-MON"), "status"]).size()


您会注意到第二周只有两个状态,第一个有六个状态。这是因为第二周内缺少零状态的jira问题。有没有一种方法可以使大小函数包括计数为零的缺失状态,以便每个星期内的数据是同一组状态?

最佳答案

您可以尝试unstackstack

enddf.unstack(level=-1, fill_value=0).stack()


输出:

                           2
0          1
2019-11-04 Authorize Work  4
           Await Work      1
           Check Work      4
           Closed          4
           Confirm Work    3
           Do Work         3
2019-11-11 Authorize Work  6
           Await Work      0
           Check Work      0
           Closed          0
           Confirm Work    0
           Do Work         2

关于python - 使用pandas size()函数包含零计数的项目吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58824615/

10-11 23:23