0

I have a pandas dataframe:

Overall Language
Hello en
Hola es

I would like to create another column - Overall Correct - where the text is translated to english. Thus, the condition is that if the language is equal to "en", then do not translate. Else, translate from Spanish to English.

Any suggestion on how to do that? I am trying to use this code with no luck:

from googletrans import Translator
translator = Translator()
df['Overall Correct'] = [df['Overall'] if x == 'en' else translator.translate(df['Overall'], src='es', dest='en') for x in df['Language']]
Stefano
  • 169
  • 6
  • 16
  • Look here for information about applying function to multiple columns in pandas dataframe: https://stackoverflow.com/questions/13331698/how-to-apply-a-function-to-two-columns-of-pandas-dataframe – STerliakov Apr 22 '21 at 17:35
  • 1
    For your case it will be `df['Overall_correct'] = df[['Overall', 'Language']].apply(lambda row: (row[0] if row[1]=='en' else translator.translate(row[0], source='es', dest='en')), axis=1)` – STerliakov Apr 22 '21 at 17:38
  • Thanks, @SUTerliakov it works! do you know how to get just the translated text as output? Running that code, I get this string "Translated(src=es, dest=en, text=Hello)", though I would like to see just "Hello" as output. I also tried: `df['Overall_correct'] = df[['Overall', 'Language']].apply(lambda row: (row[0] if row[1]=='en' else translator.translate(row[0], source='es', dest='en').text), axis=1)` but in this way, it does not translate the text leaving it as "Hola" – Stefano Apr 22 '21 at 18:02
  • Use `translator.translate(...).text` in lambda. – STerliakov Apr 22 '21 at 18:06

1 Answers1

0

You need to use apply

df["Overall Correct"] = df.apply(lambda x:  x["Overall"] if x["Overall"] == "en" else 
 translator.translate(df['Overall'], src='es', dest='en') ,axis=1)
Yefet
  • 2,010
  • 1
  • 10
  • 19