我有一个数据集,其中包含一些列,这些列是一个项目列表。下面我举了一个例子。我正在尝试查找列表中有100%匹配项的条目。我想找90%或更低的。你知道吗
>>> df2 = pd.DataFrame({ 'ID':['1', '2', '3', '4', '5', '6', '7', '8'], 'Productdetailed': [['Phone', 'Watch', 'Pen'], ['Pencil', 'fork', 'Eraser'], ['Apple', 'Mango', 'Orange'], ['Something', 'Nothing', 'Everything'], ['Eraser', 'fork', 'Pencil'], ['Phone', 'Watch', 'Pen'],['Apple', 'Mango'], ['Pen', 'Phone', 'Watch']]})
>>> df2
ID Productdetailed
0 1 [Phone, Watch, Pen]
1 2 [Pencil, fork, Eraser]
2 3 [Apple, Mango, Orange]
3 4 [Something, Nothing, Everything]
4 5 [Eraser, fork, Pencil]
5 6 [Phone, Watch, Pen]
6 7 [Apple, Mango]
7 8 [Pen, Phone, Watch]
如果注意到df2
中的索引0和索引7,则它们具有相同的项集,但顺序不同。其中索引0和索引5具有相同顺序的相同项。我想把他们两个看作是一对。我试过groupby
和series.isin()
。我还尝试将数据集拆分为两个数据集,但由于类型错误而失败。你知道吗
首先,我想计算完全匹配的项的数量(匹配行的数量也可以)以及它匹配到的行索引号。但是当有像df2中的索引2和索引6这样只有部分匹配的项时。我想说的是已经匹配的项目的百分比,以及与之对应的列号。你知道吗
我提到过。我试图将特定列值的数据分为两部分。那么
applied df2['Intersection'] =
[list(set(a).intersection(set(b)))
for a, b in zip(df2_part1.Productdetailed, df2_part2.Productdetailed)
]
,其中a
和b
是来自df2_part1
和df2_part2
的碎片的Productdetailed
列。你知道吗
有办法吗?请帮忙
此解决方案解决了精确匹配任务(代码复杂度非常高,不建议使用):
对于完全匹配和部分匹配使用(如果至少有2个值匹配,则部分匹配也可以更改):
要知道精确匹配:
Hit是出现的次数。 结果df3:
部分匹配比较困难,但您可以开始拆分列表并使用数据透视表:
如果你运行测试。在“list column”中有“Productdetailed column”列表中的单词。还有,你有身份证。。。所以我认为使用pivot表可以提取信息。。你知道吗
相关问题 更多 >
编程相关推荐