简体   繁体   中英

Pandas isin based on two columns

Suppose I have the following two dataframes:

df = pd.DataFrame({'col1':['a','b', 'c'], 'col2': ['q', 'w', 'e']})

df1 = pd.DataFrame({'col1': ['c', 'b', 'b', 'r','a', 'l', 'c'], 'col2': ['e', 'w', 'q', 'u', 'q', 'w', 'q']})
print(df)
col1    col2
a   q
b   w
c   e

print(df1)
col1 col2
   c    e
   b    w
   b    q
   r    u
   a    q
   l    w
   c    q

I want to find all pairs in df1 which are in df and return in a new column, my expected output would be:

col1 col2   col3
   c    e   True
   b    w   True
   b    q  False
   r    u  False
   a    q   True
   l    w  False
   c    q  False

I know it can be done by concatenating the columns by creating a key but I would like to now if there is a concise way to do it, something like:

df1.isin(df) #doesn't work

We have merge with indicator

s=df1.merge(df,indicator=True,how='left')
s['col3']=s['_merge']=='both'
s
  col1 col2     _merge
0    c    e       both
1    b    w       both
2    b    q  left_only
3    r    u  left_only
4    a    q       both
5    l    w  left_only
6    c    q  left_only

Here is my contribution:

df1['col3'] = None
for key, tup in enumerate(list(zip(df1.col1,df1.col2))):
    if tup in list(zip(df.col1,df.col2)):
        df1['col3'].iloc[key] = True
    else:
        df1['col3'].iloc[key] = False

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