Reputation: 61
I have a dataframe like :
df= [ ID child_ID STATUS1
123 11 OK
123 22 KO
123 23 OK
124 56 OK
124 45 OK
I want to get a final status by ID,if one of the child_id is KO it means that my finalstatus is KO so i want a dataframe like :
f= [ ID Child_ID STATUS1 Statusfinal
123 11 OK KO
123 22 KO KO
123 23 OK KO
124 56 OK OK
124 45 OK OK
how can i do it ?
Upvotes: 1
Views: 289
Reputation: 3528
You can do:
g = df.groupby('ID')
df['Statusfinal'] = np.where((g['STATUS1'].transform(lambda x: x.eq('KO').any())), 'KO',df['STATUS1'])
output
df['Statusfinal']
Out[2]:
0 KO
1 KO
2 KO
3 OK
4 OK
Name: Statusfinal, dtype: object
Or:
As pointed out in the comment by piRSquared:
df['Statusfinal'] = np.where(df.STATUS1.eq('KO').groupby(df.ID).transform('any'), 'KO', df['STATUS1'])
Upvotes: 1