在我的一个小项目中,我遇到了以下问题.我有一个很大的数据集,其中一些字符串值意外地没有正确写入.我的目标是编写一个函数,确保在循环中查找所有看起来非常相似的名称(0.75),并且将获得相同的名称.在下面的示例中,我描述了数据的一个子集,其中"Bob Fisherman"、"Bob Felony"和"Bob Haris"是正确的名称.我希望将拼错的名字更改为上面的名称,如果它们匹配的话
以下是数据帧的一个子集:
columns = ["Name", "Type","Amount", "Year"]
data = [("Bob fisherman", "Income", 150, 2022), ("Bob fisherman","Income", 100, 2021), ("Bob Felony", "Income", 100, 2021), ("Bob Felany", "Expense", 50, 2022), ("Bob Haris", "Expense", 100, 2022), ("Bob Disherman", "Expense", 100, 2021)]
data = spark.createDataFrame(data).toDF(*columns)
所以最终我想要这样的东西:
Name | Type | Amount | Year |
---|---|---|---|
Bob Fisherman | Income | 150 | 2022 |
Bob Fisherman | Income | 100 | 2021 |
Bob Felony | Income | 100 | 2021 |
Bob Felany | Income | 50 | 2022 |
Bob Haris | Income | 100 | 2022 |
Bob Felony | Income | 100 | 2021 |
Bob Fisherman | Income | 100 | 2022 |
在本例中,它只涉及Bob.但是在总的样本中,我有更多的名字,所以使用预先指定的列表不会很不幸地减少它.
我试着从下面的问题中获得一些灵感,但似乎没有奏效: Replace similar strings in a column with the same string个