I have a dataframe df I want check each value in Age column and compare it with diff column and finally assign Yes, No or other to Is_dob column.
Age diff(DOB-today) Is_dob
54 54 Yes
27 27 Yes
43 44 No
68 70 No
NA NA Other
I have tried below code:
for i, j in df['Age'].iteritems():
for k, m in df['diff'].iteritems()
if pd.isnull(j) or pd.isnull(m):
df['Is_dob']='other'
elif(m==j):
df['Is_dob']='Yes'
else:
df['Is_dob']='No'
But not getting expected output. Can anyone suggest solution?
CodePudding user response:
Use numpy.select
with 2 conditions and default value:
df['Is_dob'] = np.select([df['Age'].isna() | df['diff'].isna(),
df['Age'].eq(df['diff'])], ['Other','Yes'], default='No')
print (df)
Age diff Is_dob
0 54.0 54.0 Yes
1 27.0 27.0 Yes
2 43.0 44.0 No
3 68.0 70.0 No
4 NaN NaN Other
Or subtract values and then test:
sub = df['Age'].sub(df['diff'])
df['Is_dob'] = np.select([sub.isna(), sub.eq(0)], ['Other','Yes'], default='No')
print (df)
Age diff Is_dob
0 54.0 54.0 Yes
1 27.0 27.0 Yes
2 43.0 44.0 No
3 68.0 70.0 No
4 NaN NaN Other