Python Forum

Full Version: Fuzzy Wuzzy how to get my fuzzyratio displayed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I am a python novice. I am trying to use FuzzyWuzzy to compare the text in 2 columns. I have imported my csv with just the 2 columns I want to compare. I have written this code and it runs:-

def compute_fuzz_ratio():
return fuzz.ratio(['Column1Heading'], ['Column2Heading'])

however when I add df['FuzzRatio'] = df.apply(compute_fuzz_ratio, axis=1) to try and get the ratio displayed in the dataframe I get this error "TypeError: object of type 'float' has no len()" can anyone let me know what I am doing wrong?

Many thanks
from fuzzywuzzy import fuzz

df['Ratio'] = df.apply(lambda x: fuzz.ratio(x.A, x.B), axis=1)
where A and B are the columns you want to compare.
Thank you for your response, really appreciated. I've put that code in but I am still getting that same error "TypeError: object of type 'float' has no len()"
Please post the entire error message, including the traceback. My guess is that one of the columns contains floats.

This works:
from fuzzywuzzy import fuzz
import pandas as pd

df = pd.DataFrame({
    "A": "This is a test".split(), "B": "That was a test".split()
})
df["Ratio"] = df.apply(lambda x: fuzz.ratio(x.A, x.B), axis=1)
print(df)
Output:
A B Ratio 0 This That 50 1 is was 40 2 a a 100 3 test test 100
This fails and gives me the error you are seeing.
from fuzzywuzzy import fuzz
import pandas as pd

df = pd.DataFrame({
    "A": "This is a test".split(), "B": [1.0, 2, 3, 4]
})
df["Ratio"] = df.apply(lambda x: fuzz.ratio(x.A, x.B), axis=1)
print(df)
Error:
TypeError: object of type 'float' has no len()