SFW
SFW

Reputation: 31

Applying Textblob to dataframe returns characters instead of textblob object

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

Answers (1)

Muhammad Shifa
Muhammad Shifa

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)

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

Upvotes: 1

Related Questions