[英]Intersection / union for each set of words in any given two lists python in a for loop
[英]intersection and union set between two lists base on data frame
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]]
}
基于数据框:如何找到B列和C之间的交集和并集? output 这样的:
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]
您可以定义一次返回两个值的自定义 function 并逐行应用该 function。
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.