'str.contains function AND does not contain

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?



Solution 1:[1]

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))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sagi