Comparing entries in dataframes in pandas

I have 2 identical dataframes, we can use this as an example.

import pandas as pd
import numpy as np

data = {'name': ['Sheldon', 'Penny', 'Amy', 'Penny', 'Raj', 'Sheldon'],
                'episodes': [42, 24, 31, 29, 37, 40],
                'gender': ['male', 'female', 'female', 'female', 'male', 'male']}
data1 = {'name': ['Sheldon', 'Penny', 'Amy', 'Penny', 'Raj', 'Sheldon'],
                'episodes': [12, 32, 31, 32, 37, 40],
                'gender': ['male', 'female', 'female', 'female', 'male', 'male']}    

df1 = pd.DataFrame(data1, columns = ['name','episodes', 'gender'])    
df = pd.DataFrame(data, columns = ['name','episodes', 'gender'])
for names in df['name']:
    if (df[df['name'].str.contains(f'{names}')]['episodes']).any() == (df1[df1['name'].str.contains(f'{names}')]['episodes']).any():
        print('True')
        
    else:
        print('False')

It is checking if the number of episodes are different between the two dataframes and should print false where they are different episodes. But I am getting all True

True
True
True
True
True
True

Why is it not printing false?

Answer

Use set_index then compare episodes columns:

>>> df.set_index('name')['episodes'] == df1.set_index('name')['episodes']
name
Sheldon    False
Penny      False
Amy         True
Penny      False
Raj         True
Sheldon     True
Name: episodes, dtype: bool