5

I am having an excel file where the "value" column contains different language statements. I want to translate the entire value column into English.

enter image description here

For testing purpose I'm using the below code, but it's throwing some exception

import pandas as pd
from googletrans import Translator
exl_file = 'ipfile1.xlsx'
df = pd.read_excel(exl_file)
print(df)

translator = Translator()
df1 = df['value'].apply(translator.translate, src='es', dest='en').apply(getattr, args=('text',))
print(df1)

Can you please guide how to apply translator on each rows to convert into English?

Nabarun Chakraborti
  • 219
  • 1
  • 5
  • 14
  • Next time you should include the exception from your code. This question was simple enough, but tracebacks are useful for debugging – oh_my_lawdy Apr 28 '21 at 20:41

2 Answers2

4

You can .apply the translator to the value column like this:

df['translated_value'] = df['value'].apply(lambda x: translator.translate(x, dest='en').text)
oh_my_lawdy
  • 449
  • 4
  • 15
  • 2
    I was getting some exception which is resolved by installing google_trans_new and few changes in the code. like: from google_trans_new import google_translator translator = google_translator() df['translated_value'] = df['value'].apply(lambda x: translator.translate(x, lang_tgt='en')) – Nabarun Chakraborti Apr 29 '21 at 04:41
  • Google translate was not working for me. But 'pinyin' worked for me. df['translated_value'] = df['value'].apply(lambda x: pinyin.get(x, format="strip", delimiter=" ")) – R S John Apr 28 '22 at 01:32
2

Google Translate has limits on volume translated. EasyNMT is a scalable solution.

from easynmt import EasyNMT
model = EasyNMT("opus-mt")

df["value_en"] = df.apply(lambda row: model.translate(row["value"], target_lang="en"), axis=1)
Saurabh Khanna
  • 531
  • 4
  • 4