Chadee Fouad
Chadee Fouad

Reputation: 2948

Pandas test if one column is a subset of another column? (i.e. column B contains column A)

data = {'COL1':['United States of America', 'United Kingdom'], 'COL2':['States of America', 'United States']} 
df = pd.DataFrame(data)  
df['IS_COL1_IN_COL2'] = df['COL1'].isin(df['COL2'])
display(df)

The code above is giving the result below. I expected the values to be False, but I'm getting True? Can you please let me know what's wrong in my code? Thanks.

enter image description here

Upvotes: 2

Views: 728

Answers (1)

jezrael
jezrael

Reputation: 862851

I think you need in with lambda function:

df['IS_COL1_IN_COL2'] = df.apply(lambda x:x['COL1'] in x['COL2'], axis=1)
df['IS_COL2_IN_COL1'] = df.apply(lambda x:x['COL2'] in x['COL1'], axis=1)

print (df)
                       COL1               COL2  IS_COL1_IN_COL2  \
0  United States of America  States of America            False   
1            United Kingdom      United States            False   

   IS_COL2_IN_COL1  
0             True  
1            False  

Function Series.isin working different - it compare each value of COL1 by all values of COL2, check changed data sample:

data = {'COL1':['United States of America', 'United Kingdom','USA','JAR'], 
        'COL2':['States of America', 'United States','UK', 'USA'],} 
df = pd.DataFrame(data)  

df['IS_COL1_IN_COL2_isin'] = df['COL1'].isin(df['COL2'])
df['IS_COL1_IN_COL2'] = df.apply(lambda x:x['COL1'] in x['COL2'], axis=1)
df['IS_COL2_IN_COL1'] = df.apply(lambda x:x['COL2'] in x['COL1'], axis=1)
print (df)
                       COL1               COL2  IS_COL1_IN_COL2_isin  \
0  United States of America  States of America                 False   
1            United Kingdom      United States                 False   
2                       USA                 UK                  True   
3                       JAR                USA                 False   

   IS_COL1_IN_COL2  IS_COL2_IN_COL1  
0            False             True  
1            False            False  
2            False            False  
3            False            False  

Upvotes: 2

Related Questions