Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Not selecting a row if substring of A is in string of B of one DF

I have a df with "Employee Common Name" and "DisplayName" I would like to NOT select the row if the "Employee Common Name" is within the "DisplayName".

I have not found a way to do that yet but my current (failing) work around is to create a Boolean Column if the "Employee Common Name" is within the "DisplayName".

However everything is false.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Working Example:


import pandas as pd
df = pd.DataFrame({'Employee Common Name': ['Bob', 'Makenzie', 'Alice'], 
                   'DisplayName': ['Robert Inger', 'Kenzie Doe', 'Alice Cooper']})

df['Allowed']=df.apply(lambda x: str(df['Employee Common Name']) in str(df['DisplayName']).lower(), axis=1)

Expected Output:

Employee Common Name DisplayName Allowed
Bob Robert Inger False
Makenzie Kenzie Doe False
Alice Alice Cooper True

I already used str.strip() on ‘Employee Common Name"

>Solution :

You did good work, there is a tiny mistake. You never used x (the argument of your lambda function). Use x to access the current row instead of df (accessing the whole series).

Also, as pointed out in other answers and comment, you need to apply .lower() to either to both strings or to none of them.

import pandas as pd
df = pd.DataFrame({'Employee Common Name': ['Bob', 'Makenzie', 'Alice'], 
                   'DisplayName': ['Robert Inger', 'Kenzie Doe', 'Alice Cooper']})


df['Allowed'] = df.apply(
    lambda x: x['Employee Common Name'].lower() in x['DisplayName'].lower(), axis=1
)

output as expected

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading