简体   繁体   中英

Check if one column value is in another column and create column to indicate in Pandas

I want to check if A in Table1 is found in A1 in Table2 and create an indicator column in Table1 to show this result.

Table1 :

A           B       C       G
Harry       pig     NY      Year1
Kasey       cat     AL      Year2
Ron         b       AK      Year1
Amy         axe     KY      Year4
...

Table2 :

A1          Value       
John        X     
Mark        Y   
Harry       Z       
Ron         Z  
...   

Expected output:

A           B       C       G        exists
Harry       pig     NY      Year1    1
Kasey       cat     AL      Year2    0
Ron         b       AK      Year1    1
Amy         axe     KY      Year4    0
...

Here is a solution you can try out, using np.where

import numpy as np

df1['exists'] = np.where(df1['A'].isin(df2['A1'].unique()), 1, 0)

       A    B   C      G  exists
0  Harry  pig  NY  Year1       1
1  Kasey  cat  AL  Year2       0
2    Ron    b  AK  Year1       1
3    Amy  axe  KY  Year4       0

Another option is merge :

out = pd.merge(df1, df2['A1'].drop_duplicates(),
               left_on='A', right_on='A1', how='left') \
    .rename(columns={'A1': 'exists'})

out['exists'] = out['exists'].notnull().astype(int)

print(out)

Output:

       A    B   C      G  exists
0  Harry  pig  NY  Year1       1
1  Kasey  cat  AL  Year2       0
2    Ron    b  AK  Year1       1
3    Amy  axe  KY  Year4       0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM