【发布时间】:2020-06-08 15:02:40
【问题描述】:
我有一个 df,如下所示。我正在尝试根据主机列的值查找行的交集。
host values
test ['A','B','C','D']
test ['D','E','B','F']
prod ['1','2','A','D','E']
prod []
prod ['2']
预期输出是第一行与下一行的交集 如果主机值相同。 对于上面的 df,输出将是
test=['B','D'] - intersection of row 1 and 2
prod=[] - intersection of row 3 and 4
prod=[] - intersection of row 4 and 5
由于主机列值不匹配,不执行第 2 行和第 3 行的交集。任何帮助表示赞赏。
df.to_dict() 值为
{'host': {0: 'test', 1: 'test', 2: 'prod', 3: 'prod', 4: 'prod'},
'values': {0: ['A', 'B', 'C', 'D'],
1: ['D', 'E', 'B', 'F'],
2: ['1', '2', 'A', 'D', 'E'],
3: [],
4: ['2']}
}
【问题讨论】:
标签: python python-3.x pandas dataframe intersection