I would like to translate df['Comments']
from whatever language they are in to English and store it in df['Comments_translated']
,and just copy paste the value in df['Comments']
to df['Comments_translated']
if the comment is already in English.
import pandas as pd
data = {'text_language': ['en', 'de','it','unknown', 'ru'],
'Comments': ['Hello World', 'Hallo Welt', 'Ciao mondo','ciao mon' 'Привет мир']
}
df = pd.DataFrame (data, columns = ['text_language','Comments'])
#!pip install googletrans
from googletrans import Translator
translator = Translator()
for row in df['Comments']:
if df[(df['text_language'] !='en')]:
df['Comments_translated'] = df['Comments'].apply(translator.translate, dest='en')
else:
df['Comments_translated'] = df['Comments']
The code returns:
ValueError Traceback (most recent call last)
<ipython-input-24-13044dcbf944> in <module>()
3 translator = Translator()
4 for row in TMcopy['Comments']:
----> 5 if TMcopy.loc[(TMcopy['text_language'] !='en')]:
ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I have taken a look at this Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() one, but my case, while also a conditional statement, does not contain any numerical values and does not require & | operator..