I'm iterating over all values in columns that contain 'Applicant Age' and adding them into a new column.
for i in range(17):
rule_col = f'rule{i}_evaluationstring'
matches = df_merged[rule_col].astype(str).str.contains('Applicant Age', na=False)
df_merged.loc[matches, 'applicant_age'] = df_merged.loc[matches, rule_col]
This works properly, but there are some entries that have an 'Applicant Age = 200' that I would like to omit.
How do I add an additional function that omits all values that contain 200 while keeping the functionality of the above?
CodePudding user response:
You can use the bitwise operators &
(and) and ~
(not) in combination.
The syntax looks like df.loc[(condition_A) & (~condition_B)]
An example relevant for your question would be:
df = df_merged
selected = df.loc[
(df[rule_col].astype(str).str.contains('Applicant Age', na=False)) &
(~df[rule_col].astype(str).str.contains('200', na=False))
]
CodePudding user response:
Try this:
matches = (df_merged[rule_col].astype(str).str.contains('Applicant Age', na=False)) & (~df_merged[rule_col].astype(str).str.contains('Applicant Age = 200', na=False))