大熊猫滚动x天与groupby有条件的累积计数

时间:2018-12-17 07:31:31

标签: python pandas dataframe

我有这个数据框,我想按特定列分组滚动条件计数

输入

col_groupby |  status  |    date
--------------------------------------
    A       | SUCCESS  |   2018-01-01
    A       | FAILED   |   2018-01-01
    B       | SUCCESS  |   2018-01-02
    B       | SUCCESS  |   2018-01-03
    A       | FAILED   |   2018-01-06

输出

col_groupby |  status  | status_cumcount | success_cumcount |    date
-------------------------------------------------------------------------
    A       | SUCCESS  |        1        |        1         |  2018-01-01
    A       | FAILED   |        2        |        1         |  2018-01-01
    B       | SUCCESS  |        1        |        1         |  2018-01-02
    B       | SUCCESS  |        2        |        2         |  2018-01-03
    A       | FAILED   |        1        |        0         |  2018-01-06

status_cumcount:对于col_groupby分组的每一个状态,累计1天。 success_cumcount:每累积1天的成功状态累积一次。

所以在第五行中,过去1天的状态A的计数为1,成功状态累计为0。

我该如何实现? 我试过了: df.groupby('col_groupby').rolling('1d', on='date').status.count(),但没有得到我期望的结果。

它在数字列上有效。如果我要计算数量,例如: df.groupby('col_groupby').rolling('1d', on='date').amount.count()。 它将起作用。

请帮助。

0 个答案:

没有答案
相关问题