Having a dataframe as below
df1 = pd.DataFrame({'Name1':['A','B','A','B','B','C','C','C','E','E','E'],
'Name2':['A','B','D','C','D','D','A','B','A','B','C'],'Marks2':['Nan','Nan',6,50, 88,'Nan',140,9,'Nan',65,70]})
df1
if Name1 and Name2 are equal, then need to replace Marks2 with 0.The rest of the values need to be same.
CodePudding user response:
df['Marks2']=df['Marks2'].mask(df['Name1'].eq(df['Name2']), 0)
df
Name1 Name2 Marks2
0 A A 0
1 B B 0
2 A D 6
3 B C 50
4 B D 88
5 C D Nan
6 C A 140
7 C B 9
8 E A Nan
9 E B 65
10 E C 70
CodePudding user response:
If you want to always replace Marks2 (regardles of it being null or not) then:
df1.loc[df1["Name1"] == df1["Name2"], "Marks2"] = 0
If instead you only want to set Marks2 to 0 if it was null then:
df1.loc[(df1["Name1"] == df1["Name2"]) & df1["Marks2"].isna(),"Marks2"] = 0
CodePudding user response:
You can use .loc[]
to filter on the rows where Name1 and Name2 are equal like so:
df1.loc[df1['Name1'] == df1['Name2']]
You can then specify the mark column, and set it to 0 when the above is the case
df1.loc[df1['Name1'] == df1['Name2'], 'Marks2'] = 0
Name1 Name2 Marks2
0 A A 0
1 B B 0
2 A D 6
3 B C 50
4 B D 88
5 C D Nan
6 C A 140
7 C B 9
8 E A Nan
9 E B 65
10 E C 70