如何创建包含组规定的条件计数的列

时间:2019-07-10 17:44:55

标签: python pandas

我正在尝试计算数字分组中未关闭status_2的每个实例

df['conditional_count'] = df.groupby(['no_',(df['status_2']!=('closed'))]).transform('count')

no_ status 1    status 2
1   Opened  Opened
1   Opened  Closed
3   Opened  Opened
4   Opened  Opened
5   Opened  Opened
5   Opened  Opened
6   Opened  Opened

我希望表格看起来像这样:

no_ status 1    status 2    Count
1   Opened  Opened  1
1   Opened  Closed  1
3   Opened  Opened  1
4   Opened  Opened  1
5   Opened  Opened  2
5   Opened  Opened  2
6   Opened  Opened  1

0 个答案:

没有答案