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