简体   繁体   中英

Find intersection of two sets of columns in python pandas dataframe for each row without looping

I have a following pandas.DataFrame :

df = pd.DataFrame({'A1':['a','a','d'], 'A2':['b','c','c'], 
                   'B1':['d','a','c'], 'B2': ['e','d','e']})
  A1 A2 B1 B2
0  a  b  d  e
1  a  c  a  d
2  d  c  c  e

I would like to choose the rows in which values in A1 and A2 are different from B1 and B2 , or intersection of values in ['A1', 'A2'] and ['B1', 'B2'] is empty, so in the above example only the row 0 should be chosen.

So far the best I could do is to loop over every row of my data frame with the following code

for i in df.index.values:
   if df.loc[i,['A1','A2']].isin(df.loc[i,['B1','B2']]).sum()>0:
       df = df.drop(i,0)

Is there a way to do this without looping?

You can test for that directly like:

Code:

df[(df.A1 != df.B1) & (df.A2 != df.B2) & (df.A1 != df.B2) & (df.A2 != df.B1)]

Test Code:

df = pd.DataFrame({'A1': ['a', 'a', 'd'], 'A2': ['b', 'c', 'c'],
                   'B1': ['d', 'a', 'c'], 'B2': ['e', 'd', 'e']})

print(df)
print(df[(df.A1 != df.B1) & (df.A2 != df.B2) & 
         (df.A1 != df.B2) & (df.A2 != df.B1)])

Results:

  A1 A2 B1 B2
0  a  b  d  e
1  a  c  a  d
2  d  c  c  e

  A1 A2 B1 B2
0  a  b  d  e

By using intersection

df['Key1']=df[['A1','A2']].values.tolist() 
df['Key2']=df[['B1','B2']].values.tolist() 


df.apply(lambda x : len(set(x['Key1']).intersection(x['Key2']))==0,axis=1)
Out[517]: 
0     True
1    False
2    False
dtype: bool


df[df.apply(lambda x : len(set(x['Key1']).intersection(x['Key2']))==0,axis=1)].drop(['Key1','Key2'],1)
Out[518]: 
  A1 A2 B1 B2
0  a  b  d  e

In today's edition of
Way More Complicated Than It Needs To Be

Chapter 1
We bring you map , generators, and set logic

mask = list(map(lambda x: not bool(x),
         (set.intersection(*map(set, pair))
          for pair in df.values.reshape(-1, 2, 2).tolist())
        ))

df[mask]

  A1 A2 B1 B2
0  a  b  d  e

Chapter 2
Numpy broadcasting

v = df.values
df[(v[:, :2, None] != v[:, None, 2:]).all((1, 2))]

  A1 A2 B1 B2
0  a  b  d  e

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