1

I am new to Pandas and Python.

My dataframe:

df

Text
Best tv in 2020
utilizar un servicio sms gratuito
utiliser un tv pour netflix

My desired output

Text                                    Language
Best tv in 2020                         en
utilizar un servicio sms gratuito       es
utiliser un tv pour netflix             fr

What I am using:

from textblob import TextBlob

b = TextBlob("utilizar un servicio sms gratuito")
print(b.detect_language())

>>es

I am not sure how I could integrate this method to fill my Pandas Dataframe.

I have tried:

df['Language'] = TextBlob(df['Text']).detect_language()

But I am getting an error:

TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'pandas.core.series.Series'>

I understand what it means, that I need to pass a string rather than pandas DataFrame Series, so my question is how would I loop the entire Series to detect language per row in column text?

Thank you for your suggestions.

1 Answer 1

3

Use Series.apply with lambda function:

df['Language'] = df['Text'].apply(lambda x: TextBlob(x).detect_language())

Or Series.map:

df['Language'] = df['Text'].map(lambda x: TextBlob(x).detect_language())

print (df)
                                Text Language
0                    Best tv in 2020       en
1  utilizar un servicio sms gratuito       es
2        utiliser un tv pour netflix       fr
Sign up to request clarification or add additional context in comments.

4 Comments

Hi @jezrael and thank you as always. Any comments on the performance difference between these two solutions?
@JonasPalačionis - First is more common for pass functions, but not working for index values, second is used for df.index.map, also working for columns like in answer.
understood, thanks. Not sure if you have worked with textblob before, but now I am getting urllib.error.HTTPError: HTTP Error 429: Too Many Requests, any idea for workarounds?
@JonasPalačionis - I working with it first time, but I guess there is some protection for avoid many requests...

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.