Reputation: 31
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?
Upvotes: 1
Views: 309
Reputation: 132
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)
If you don't use the str()
like:
df['newText'] = df['text'].apply(lambda x:(TextBlob(x)))
Upvotes: 1