Applying Textblob to dataframe returns characters instead of textblob object

241 Views Asked by At

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

There are 1 best solutions below

0
Muhammad Shifa On

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