0
a = {'A' : [1,2,3,4],
'B' : [[1,4,5,6],[2,3,6],[4,5,6]],
'C' : [[1,4,6],[3,5],[4,10],[10]]
}

Base on dataframes: How to find the intersection and union set between column B and C? the output like that:

    A      B           C          intersect       union
0   1   [1,4,5,6]    [1,4,6]      [1,4,6]        [1,4,5,6]
1   2   [2,3,6]      [3,5]        [3]            [2,3,5,6]
2   3   [4,5,6]      [4,10]       [4]            [4,5,6,10]
3   4   [4,5,6]      [10]         []             [4,5,6,10]
Rabinzel
  • 7,757
  • 3
  • 10
  • 30

1 Answers1

1

You can define a custom function that returns two values at a time and apply that function rowwise.

def func(row):
    inters = list(set(row['B']).intersection(row['C']))
    uni = list(set(row['B']).union(row['C']))
    return inters, uni

a[['intersect', 'union']] = a.apply(func, axis=1, result_type='expand')
print(df)
   A             B          C  intersect          union
0  1  [1, 4, 5, 6]  [1, 4, 6]  [1, 4, 6]   [1, 4, 5, 6]
1  2     [2, 3, 6]     [3, 5]        [3]   [2, 3, 5, 6]
2  3     [4, 5, 6]    [4, 10]        [4]  [10, 4, 5, 6]
3  4     [4, 5, 6]       [10]         []  [10, 4, 5, 6]
Rabinzel
  • 7,757
  • 3
  • 10
  • 30
  • Thanks for your answer. is there any other way without using pandas. – ice-coconut Nov 16 '22 at 14:04
  • Didn't you say you have a dataframe? Also you talked about "column" `B` and `C`. I just assumed that this is a pandas dataframe... So you only have a dictionary and would like to create two new keys with `intersection` and `union` in it? – Rabinzel Nov 16 '22 at 14:09