Mostafa Gafer
Mostafa Gafer

Reputation: 73

Text translation with Pandas

I am trying to translate a text column using python which contain different text of different languages . nothing fancy with my code yet .

import pandas as pd
df = pd.read_excel('D:/path', head=None)

I used the following code :

from googletrans import Translator
translator = Translator()
df['Text to English'] = df['Text'].apply(translator.translate, src='id', dest='en')

but it gave me an error :

AttributeError: 'NoneType' object has no attribute 'group'

I search more for any other code and I came up with :

from textblob import TextBlob
df['Text to English'] = df['Text'].str.encode('ascii', 'ignore').apply(lambda x: TextBlob(x.strip()).translate(to='en'))

but it gave me an error of : TypeError: cannot use a string pattern on a bytes-like object

is there any solution for this ?? and thanks in advance

Upvotes: 2

Views: 2797

Answers (1)

jezrael
jezrael

Reputation: 862781

I think there are None or NaNs values, so is possible filter them by notna:

mask = df['Text'].notna()
df.loc[mask,'Text to English'] = df.loc[mask, 'Text'].apply(translator.translate, 
                                                            src='id', dest='en')

Upvotes: 1

Related Questions