基于条件的自定义标志



我有一个数据集


条件无错误//tr>无错误>无错误[/tr>无错误
id 引用 名称
1 123 a
1 456 b 错误
1 789 c
2 231 d无错误
2 312 e
2 546 f 无错误
3 645 g 错误
3 879 h 错误
4 789 i
4 978 j 无错误

如果id:每个组至少有一个值error,则使用numpy.where进行测试

m = df['id'].isin(df.loc[df['conditionCol'].eq('error'), 'id'])
#alternative
#m = df['conditionCol'].eq('error').groupby(df['id']).transform('any')
df['error_flag'] = np.where(m, 'yes', 'no')
print (df)
id  ref name conditionCol error_flag
0   1  123    a     no_error        yes
1   1  456    b        error        yes
2   1  789    c     no_error        yes
3   2  231    d     no_error         no
4   2  312    e     no_error         no
5   2  546    f     no_error         no
6   3  645    g     no_error         no
7   3  879    h     no_error         no
8   4  789    i        error        yes
9   4  978    j        error        yes

最新更新