Home > Mobile >  overwriting the column rows basis the condition
overwriting the column rows basis the condition

Time:11-18

Existing Dataframe :

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

Expected Dataframe :

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

I am looking to tag score in every row of unique Id as 1 if in any row below condition is satisfied : condition1 == 'attempt' & condition2 =='pass'.

CodePudding user response:

You can try:

m1 = df['condition1'].eq('attempt')
m2 = df['condition2'].eq('pass') | df['condition2'].eq('level_1')

df['score'] = (m1 & m2)
df['score'] = df.groupby('Id')['score'].transform(lambda x: x.any().astype(int))

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