1

I am trying to apply textblob like so:

df['newtext'] = df['text'].apply(lambda x: TextBlob(x))

but that tokenizes "text" and returns (I, ', v, e, , b, e, e, n, , u, s, i, n, g,...) instead of a textblob object. Any idea why this is so?

1
  • We can join it again like this... "".join(TextBlob(x)) ... nah? Commented Sep 25, 2022 at 17:32

1 Answer 1

1

Ultimately when you are using the apply method, you are returning a TextBlob object instead of a String and Pandas doesn't know exactly what to do with it (which is why you get the weird behavior)

To solve your issue, simply surround the object being returned by TextBlob with str(), and it will work. Check the below example

import pandas as pd
from textblob import TextBlob

d = {'text': ["I'm learning", "I love coding"], 'num': [2, 4]}
df = pd.DataFrame(data=d)
print(df)

df['newText'] = df['text'].apply(lambda x:str(TextBlob(x)))
print(df)

Output will look like this:enter image description here

If you don't use the str() like:

df['newText'] = df['text'].apply(lambda x:(TextBlob(x)))

The ouput will look like:enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.