我有一个包含几列的数据集。
现在我想要的是基本上根据特定列(“名称”)计算分数,但在“id”列上分组。
_id fName lName age
0 ABCD Andrew Schulz
1 ABCD Andreww 23
2 DEFG John boy
3 DEFG Johnn boy 14
4 CDGH Bob TANNA 13
5 ABCD. Peter Parker 45
6 DEFGH Clark Kent 25
所以我正在寻找的是,对于相同的 id,我是否得到了相似的条目,所以我可以根据阈值分数值删除这些条目。就像这里,如果我为 col“fName”运行它。我应该能够根据分数阈值减少这个数据框: _id fName lName age
0 ABCD Andrew Schulz 23
2 DEFG John boy 14
4 CDGH Bob TANNA 13
5 ABCD Peter Parker 45
6 DEFG Clark Kent 25
I intend to use pyjarowinkler. If I had two independent columns (without all the group by stuff) to check, this is how I use it.
df['score'] = [distance.get_jaro_distance(x, y) for x, y in zip(df['name_1'],df['name_2'])]
df = df[df['score'] > 0.87]
有人可以建议一个pythonic和快速的方法来做到这一点更新
因此,我尝试为此使用记录链接库。我最终得到了一个包含一对相似索引的数据帧,称为“匹配”。现在我只想基本上结合数据。
# Indexation step
indexer = recordlinkage.Index()
indexer.block(left_on='_id')
candidate_links = indexer.index(df)
# Comparison step
compare_cl = recordlinkage.Compare()
compare_cl.string('fName', 'fName', method='jarowinkler', threshold=threshold, label='full_name')
features = compare_cl.compute(candidate_links, df)
# Classification step
matches = features[features.sum(axis=1) >= 1]
print(len(matches))
这是比赛的样子:index1 index2 fName
0 1 1.0
2 3 1.0
我需要有人建议一种方法,以从相似行中获取数据的方式组合相似的行
最佳答案
只是想澄清一些关于你的问题的疑虑。由于声誉低,无法在评论中清除它们。
Like here if i run it for col "fName". I should be able to reduce this dataframe to based on a score threshold:
所以基本上你的函数会返回包含每个组中第一行的数据帧(按 ID)?这将导致上面列出的结果 DataFrame。
_id fName lName age
0 ABCD Andrew Schulz 23
2 DEFG John boy 14
4 CDGH Bob TANNA 13
关于python - 对 Pandas 列中的元素应用函数,分组到另一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62548557/