Add an additional column to a panda dataframe comparing two columns

Viewed 60

I have a dataframe (df) containing two columns:

Column 1 Column 2
Apple Banana
Chicken Chicken
Dragonfruit Egg
Fish Fish

What I want to do is create a third column that says whether the results in each column are the same. For instance:

Column 1 Column 2 Same
Apple Banana No
Chicken Chicken Yes
Dragonfruit Egg No
Fish Fish Yes

I've tried: df['Same'] = df.apply(lambda row: row['Column A'] in row['Column B'],axis=1)

Which didn't work. I also tried to create a for loop but couldn't even get close to it working.

Any help you can provide would be much appreciated!

5 Answers

You can simply use np.where :

import numpy as np

df['Same'] = np.where(df['Column 1'] == df['Column 2'], 'Yes', 'No')

>>> print(df)

enter image description here

In Pandas the == operator between Series returns a new Series

So you can use:

df['Same'] = df['Column A'] == df['Column B']
df['Same'] = df['Same'].replace(True, 'Yes').replace(False, 'No')

Using vectorisation: You can make use of vectorisation to efficiently achieve the desired result. For example, you can use NumPy's where function:

df['Same'] = np.where(df['Column 1'] == df['Column 2'], 'Yes', 'No')

Using apply: If you do wish to use Pandas apply() function (e.g. as the data set is small and you wish to add additional logic) then the example below shows you how to do this to achieve the desired result.

df['Same'] = df.apply(lambda row: "Yes" if row['Column 1'] == row['Column 2'] 
                                        else "No", axis=1)

The above approach will place the value 'Yes' in the 'Same' column of the data frame if the values in 'Column 1' and 'Column 2' match, otherwise it will place the value 'No'.

Result: The result of running either of the above code examples will be as follows:

      Column 1 Column 2 Same
0        Apple   Banana   No
1      Chicken  Chicken  Yes
2  Dragonfruit      Egg   No
3         Fish     Fish  Yes

More information: You can learn more about the limitations of apply() vs approaches that utilise vectorisation by clicking here.

Use isin:

df_eq['Column1'].isin(df_eq['Column2']).replace({True:'Yes', False:'No'})

You could use the .apply(lambda) expression

df['Same'] = df.apply(lambda x: 'Yes' if x['Column 1']==x['Column 2'] else 'No', axis=1)
Related