moham
moham

Reputation: 41

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 in

Translated(src=en,dst=ar,text='the sentence in english)

Here is the code I am using

import pandas as pd
file = pd.read_excel('HS_CODES_UNITS.xlsx')
from googletrans import Translator
translator = Translator()
file['Product description in arabic'] = file['Product Description in English'].apply(translator.translate, src='en', dest='ar')

Upvotes: 0

Views: 729

Answers (2)

Moritz
Moritz

Reputation: 3225

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 import Translator
translator = 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

Upvotes: 1

mujjiga
mujjiga

Reputation: 16866

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
0   this is an apple    هذه تفاحة
1   that is a mango     هذا مانجو

Upvotes: 0

Related Questions