【问题标题】:How to change column value based on fuzzy wuzzy score如何根据模糊模糊分数更改列值
【发布时间】:2021-10-04 18:29:22
【问题描述】:

我有一个数据框,其中有一列包括汽车品牌。我想应用where和fuzzywuzzy来改进数据。例如,如果一个条目与单词 Mercedes 有 80% 的匹配,我希望将其替换为 Mercedes 单词。这就是我目前所拥有的

df = df.where(fuzz.ratio(df['make'], "Mercedes") >= 80,"Mercedes")

我也试过了

df.mask (df[fuzz.ratio(df['make'], 'Mercedes') >= 85], 'Mercedes', inplace=True)

我两次都遇到同样的错误

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

df.where 是函数https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.where.html。我想我理解了这个错误,我知道我的语法不正确,但我想不出另一种方法。同样最好的是,一个非常快速的方法是理想的,因为数据帧非常大

提前谢谢你

【问题讨论】:

    标签: python pandas dataframe fuzzywuzzy


    【解决方案1】:

    两件事:

    1. 您将整个系列传递给fuzz.ratio(这会引发错误)。使用 applyfuzz.ratio 应用于系列值
    2. df.where 替换条件为False 的值,所以使用<80

    这应该可行:

    df.where(df['make'].apply(lambda x: fuzz.ratio(x, "Mercedes")) < 80,"Mercedes")
    

    【讨论】:

    • 谢谢!效果很好
    • 不客气。如果解决了您的问题,请accept回答。
    猜你喜欢
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 2011-12-03
    • 2021-09-23
    相关资源
    最近更新 更多