使用转换计算数据框中的特定值和聚合结果

Count specific values and aggregating result in dataframe using transform

我有一个与此类似的数据框:

    Errorid  Matricule Priority
0      1        01       P1
1      2        01       P2
2      3        01       NC
3      4        02       P1
4      5        02       P4
5      6        02       EDC
6      7        02       P2

这列出了 Matricule 的所有错误及其优先级。

我想做的是计算 Matricule 的所有错误,同时排除 "NC" 和 "EDC" 并将结果放入 相同 数据帧。

结果示例:

    Errorid  Matricule Priority  NberrorsMatricule
0      1        01       P1           2
1      2        01       P2           2
2      3        01       NC           2
3      4        02       P1           3
4      5        02       P4           3
5      6        02       EDC          3
6      7        02       P2           3

我尝试了以下多种方法:

DF['NberrorsMatricule'] = DF.groupby('Matricule')['Pirority'].transform(lambda x : x.count() if x in ['P1','P2','P3','P4']) 

DF['NberrorsMatricule'] = DF.groupby('Matricule')[DF['Pirority'] in ['P1','P2','P3','P4']].transform("count")

每次我都得到一个不明确的值错误。 ValueError:一个序列的真值不明确。使用 a.empty()、a.bool()、a.item()、a.any()、a.all()。

请注意,这一项有效:

DF['NberrorsMatricule'] = DF.groupby('Matricule')['Pirority'].transform("count") 

但是显然没有过滤掉纯度

这些数据框是示例,实际上我处理大量数据(在这个数据框中出现超过 400k) 所以 如果有人能帮助我理解 transform() 的行为,以及如何高效地过滤数据那就太好了。

在此先感谢您的帮助

您可以将不匹配的值替换为缺失值 Series.where and Series.isin, so if use GroupBy.transform with GroupBy.count 它排除缺失值:

L = ['P1','P2','P3','P4']
df['NberrorsMatricule'] = (df['Priority'].where(df['Priority'].isin(L))
                                         .groupby(df['Matricule'])
                                         .transform('count'))
print (df)
   Errorid  Matricule Priority  NberrorsMatricule
0        1          1       P1                  2
1        2          1       P2                  2
2        3          1       NC                  2
3        4          2       P1                  3
4        5          2       P4                  3
5        6          2      EDC                  3
6        7          2       P2                  3

详情:

print (df['Priority'].where(df['Priority'].isin(L)))
0     P1
1     P2
2    NaN
3     P1
4     P4
5    NaN
6     P2
Name: Priority, dtype: object

另一种解决方案是通过 sum 计算匹配值,因为可以将 TrueFalse 转换为 1, 0 使用 Series.view or Series.astype:

df['NberrorsMatricule'] = (df['Priority'].isin(L)
                                         .view('i1')
                                         .groupby(df['Matricule'])
                                         .transform('sum'))
print (df)

   Errorid  Matricule Priority  NberrorsMatricule
0        1          1       P1                  2
1        2          1       P2                  2
2        3          1       NC                  2
3        4          2       P1                  3
4        5          2       P4                  3
5        6          2      EDC                  3
6        7          2       P2                  3

像这样:

In [567]:  df['NberrorsMatricule'] = df[~df.Priority.isin(['NC', 'EDC'])].\ 
     ...:                               groupby('Matricule')['Errorid']\ 
     ...:                               .transform('count')                                                                          

要删除 Nan,请使用 ffill():

In [595]: df['NberrorsMatricule'] = df['NberrorsMatricule'].ffill()                                                                                                                                         

In [596]: df                                                                                                                                                                                                
Out[596]: 
   Errorid  Matricule Priority  NberrorsMatricule
0        1          1       P1                2.0
1        2          1       P2                2.0
2        3          1       NC                2.0
3        4          2       P1                3.0
4        5          2       P4                3.0
5        6          2      EDC                3.0
6        7          2       P2                3.0