Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ch 8, applying preprocessor to dataframe type-error #201

Open
DanyaLearning opened this issue Jan 9, 2025 · 1 comment
Open

Ch 8, applying preprocessor to dataframe type-error #201

DanyaLearning opened this issue Jan 9, 2025 · 1 comment
Labels
question Further information is requested

Comments

@DanyaLearning
Copy link

df['review'] = df['review'].apply(preprocessor)

Leads to a type-error: TypeError: expected string or bytes-like object, got 'float'

With small modification, I got the code to work, by making sure the text is really a string by enforcing it with str(). Not sure if this is the proper way of doing it, but it works for me.

Here is the modified preprocessor that is executed without errors:

def preprocessor(text):`
    text = re.sub('<[^>]*>', '', str(text)) #here I use the str() function
    emoticons = re.findall(r'(?::|;|=)(?:-)?(?:\)|\(|D|P)',
                           text)
    text = (re.sub(r'[\W]+', ' ', text.lower()) +
            ' '.join(emoticons).replace('-', ''))
    return text
@rasbt
Copy link
Owner

rasbt commented Jan 30, 2025

Thanks for the note, and sorry for the late follow-up! I do think your modification would correctly fix issues where the "review" is a float. But I think there may be a bigger issue here: "review" shouldn't be a float. I wonder if there was perhaps an issue with the data frame loading before that line.

I would maybe do a df.head() and a df.tail() to just double-check that it looks as expected:

Image

@rasbt rasbt added the question Further information is requested label Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants