Home > Mobile >  updating the column basis checking the condition
updating the column basis checking the condition

Time:11-18

Id                   condition2       score
A                       pass            0
A                       fail            0
B                       pass            0
B                       level_1         0
B                       fail            0
C                       fail            0
D                       fail            0

Expected Dataframe :

Id                   condition2       score
A                       pass            1
A                       fail            1
B                       pass            1
B                       level_1         1
B                       fail            1
C                       fail            0
D                       fail            0

looking to tag score as 1 for each row of unique Id , if the condition 2 has either pass or level_1 in any of the row.

df['score'] = df.groupby('Id')['condition2'].transform(lambda x: x.eq('pass').any().astype(int))

what modifications to be done on above code

CodePudding user response:

Lets use isin to find the ids which have pass or level_1:

m = df['condition2'].isin(['pass', 'level_1'])
df['score'] = df['Id'].isin(df.loc[m, 'Id']).astype(int)

If you still want to use groupby and transform..here is the fix to your existing approach:

m = df['condition2'].isin(['pass', 'level_1'])
df['score'] = m.groupby(df['Id']).transform('any').astype(int)

  Id condition2  score
0  A       pass      1
1  A       fail      1
2  B       pass      1
3  B    level_1      1
4  B       fail      1
5  C       fail      0
6  D       fail      0

CodePudding user response:

Another possible solution, list Comprehension:

df.assign(score=[1*any(df.loc[df.Id == i,
          'condition2'].isin(['pass', 'level_1'])) for i in df.Id])

Output:

  Id condition2  score
0  A       pass      1
1  A       fail      1
2  B       pass      1
3  B    level_1      1
4  B       fail      1
5  C       fail      0
6  D       fail      0
  • Related