Skip to content Skip to sidebar Skip to footer

Data Frame Column Translation

I need to translate a column in a dataframe from english to arabic the code runs fine but it don't translate the words instead it gives me this in the new column I am translating i

Solution 1:

translator.translate returns an object of type Translated. The translated text can be accessed via the property text. Check official docs here.

Fix

df = pd.DataFrame( {'English': ['this is an apple', 'that is a mango']})
translator = Translator(service_urls=[
      'translate.google.com',
    ])
df['Arabic' ] = df['English'].apply(lambda x: translator.translate(x, src='en', dest='ar').text)
print (df)

Output:

    English             Arabic
0thisis an apple    هذه تفاحة
1   that is a mango     هذا مانجو

Solution 2:

This is a bug due to a change in the Google API. A new alpha version of googletrans with a fix was released a few minutes ago.

Install the alpha version like this:

pip install googletrans==3.1.0a0

Important thing to note: You have to specify a service url, otherwise the same error still occurs. So this should work:

from googletrans importTranslatortranslator= Translator(service_urls=['translate.googleapis.com'])
translator.translate("Der Himmel ist blau und ich mag Bananen", dest='en')

But his still returns the error (at least for me):

translator = Translator()
translator.translate("Der Himmel ist blau und ich mag Bananen", dest='en')

See the discussion here for details and updates: https://github.com/ssut/py-googletrans/pull/237

Post a Comment for "Data Frame Column Translation"