pythonpandasdataframe

Pandas dataframe - finding row comparing two cell values


I have a dataframe:

Email                | ... | Name
--------------------------------------
john.cena@gmail.com  | ... | John Cena
john.smith@gmail.com | ... | John Cena

I need to find a row, that matches Name column with Email column when

email_cell.split("@")[0] == name_cell.lower().replace(" ", ".")

I tried dataframe.loc[dataframe["Email"].str.contains(dataframe["Name"].replace(" ", "."))] and other ways but I cannot use Series as if it was a string.

Is there a way to do this?


Solution

  • Take email usernames by splitting on '@' and Convert names to lowercase and replaces spaces with dots

    df[df['Email'].str.split('@').str[0] == df['Name'].str.lower().str.replace(' ', '.')]
    

    Output

    Matched rows:
                       Email         Name
    0    john.cena@gmail.com    John Cena
    2  randy.orton@gmail.com  Randy Orton
    

    You can also try apply with lambda

    df[df.apply(lambda x: x['Email'].split('@')[0] == x['Name'].lower().replace(' ', '.'), axis=1)]