As @Erfan mention in the comment section, you don't really need a for loop or lambda to achieve this, You need np.where.
- One way with
np.where
- Or you can use
np.where with np.logical_or
DataFrame:
>>> df
Delinquency 2+ Month Change Suspect LTV
0 X
1 X
2 X
3 X
4 X
5 X LTV < 10%
Result:
1- Use OR operator as pipe | with np.where
>>> df['Exceptions'] = np.where((df['Delinquency 2+ Month Change'] == 'X') | (df['Suspect LTV'] == 'LTV < 10%'), 'Yes', 'No')
>>> df
Delinquency 2+ Month Change Suspect LTV Exceptions
0 X Yes
1 X Yes
2 X Yes
3 X Yes
4 X Yes
5 X LTV < 10% Yes
2- Second method as i mention at the start with np.where with np.logical_or
>>> df['Exceptions'] = np.where(np.logical_or(df['Delinquency 2+ Month Change'] == 'X', df['Suspect LTV'] == 'LTV < 10%'), 'yes', 'no')
>>> df
Delinquency 2+ Month Change Suspect LTV Exceptions
0 X yes
1 X yes
2 X yes
3 X yes
4 X yes
5 X LTV < 10% yes
Same but alternative with .eq rather ==
df['Exceptions'] = np.where(np.logical_or(df['Delinquency 2+ Month Change'].eq('X') ,df['Suspect LTV'].eq('LTV < 10%')), 'Yes', 'No')
OR
df['Exceptions'] = np.where(np.logical_or(df['Delinquency 2+ Month Change'].eq('X') ,df['Suspect LTV'].eq('LTV < 10%')), 'Yes', 'No')
In case, you are interested about the way with using defining a function and applying that across ..
Function definition...
def exceptions(row):
if row['Delinquency 2+ Month Change'] == 'X':
return 'Yes'
if row['Suspect LTV'] == 'LTV < 10%':
return 'Yes'
else:
return 'No'
Use the Function with DataFrame.apply() as follows..
df['Exceptions'] = df.apply(exceptions, axis=1)
print(df)
Delinquency 2+ Month Change Suspect LTV Exceptions
0 X Yes
1 X Yes
2 X Yes
3 X Yes
4 X Yes
5 X LTV < 10% Yes
for loopfor this, use:df1['Exceptions'] = np.where(df1['Delinquency 2+ Month Change'].eq('X') | df1['Suspect LTV'].eq('LTV < 10%', 'Yes', 'No')