4

How to write a function in Python that translates each row of a csv file to another language and adds the translation as another column to the same csv using pandas? The input file I have, looks like this:

enter image description here

and I would like my output to be like:

enter image description here

I started with this:

from googletrans import Translator
import pandas as pd

data = pd.read_csv('~/file/my_file.csv')[['A','B']]
df = pd.DataFrame(data, columns=['A','B','A_translation', 'B_translation'])

and for translating a single sentence the following code helps, but could you please help me to use it as a function for all rows in a csv file?

sentence = 'The quick brown fox'
translations = translator.translate(sentence, dest = 'Fr')
for translation in translations:
     tr = translation.text 
     org = translation.origin

Thanks.

hets
  • 93
  • 1
  • 7

1 Answers1

2

Something like that ?

from googletrans import Translator
import pandas as pd

headers = ['A','B','A_translation', 'B_translation']
data = pd.read_csv('./data.csv')
translator = Translator()
# Init empty dataframe with much rows as `data`
df = pd.DataFrame(index=range(0,len(data)), columns=headers)


def translate_row(row):
    ''' Translate elements A and B within `row`. '''
    a = translator.translate(row[0], dest='Fr')
    b = translator.translate(row[1], dest='Fr')
    return pd.Series([a.origin, b.origin, a.text, b.text], headers)


for i, row in enumerate(data.values):
    # Fill empty dataframe with given serie.
    df.loc[i] = translate_row(row)

print(df)
Arount
  • 9,853
  • 1
  • 30
  • 43
  • Thanks for you help. Just a question: Do you have any idea why it stops working when the number of rows exceed 500 ? – hets Sep 13 '17 at 09:39