How to translate other languages to English in pandas dataframe
Question:
I am having an excel file where the "value" column contains different language statements. I want to translate the entire value column into English.
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?
Answers:
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)
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)
I am having an excel file where the "value" column contains different language statements. I want to translate the entire value column into English.
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?
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)
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)