String compare in pandas python – Test whether two strings are equal

String compare in pandas python is used to test whether two strings (two columns) are equal. In this example lets see how to

  • Compare two strings in pandas dataframe – python (case sensitive)
  • Compare two string columns in pandas dataframe – python (case insensitive)

First let’s create a dataframe


import pandas as pd
import numpy as np

df1 = {
    'State':['Arizona','Georgia','Newyork','Indiana','Florida'],
    'State_1':['New Jersey','georgia','Newyork','Indiana','florida']}

df1 = pd.DataFrame(df1,columns=['State','State_1'])
print(df1)

df1 will be

String compare in pandas python – Test whether two strings are equal 1

 

String compare two columns – case sensitive:

Let’s compare two columns to test whether they are equal. In this method it will result true only if two columns are exactly equal (case sensitive).


df1['is_equal']= (df1['State']==df1['State_1'])
print(df1)

so resultant dataframe will be

String compare in pandas python – Test whether two strings are equal 2

 

String  compare two columns – case insensitive:

This will result true even if the columns are of different cases and with spaces (case insensitive)


df1['is_equal'] =( df1['State'].str.lower().str.replace('s/+',"") == df1['State_1'].str.lower().str.replace('s/+',""))
print(df1)

so resultant dataframe will be

String compare in pandas python – Test whether two strings are equal 3

String compare in pandas python – Test whether two strings are equal                                                                                                           String compare in pandas python – Test whether two strings are equal

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts