How to translate other languages to English in pandas dataframe
Asked Answered
A

2

6

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?

Acidimeter answered 28/4, 2021 at 15:42 Comment(1)
Next time you should include the exception from your code. This question was simple enough, but tracebacks are useful for debuggingMailman
M
5

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)
Mailman answered 28/4, 2021 at 20:40 Comment(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'))Acidimeter
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=" "))Biddick
A
3

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)
Aliform answered 8/10, 2022 at 22:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.