0

data is a pandas dataframe in which language and config['TEXT FIELD'] are columns. I want to translate certain reviews in the text column to english and I am using a function dfApply

import goslate
def dfApply(row):
    if row["langauge"] == 'en':
       return row[config['TEXT FIELD']]
    else:
       return gs.translate(row[config['TEXT FIELD']], 'en')


gs = goslate.Goslate()
data['english_text'] = data.apply(dfApply, axis=1)

But the complier shows the follwing error

KeyError: ('langauge', 'occurred at index 0')
5
  • Which line are you getting error in? Sep 3, 2015 at 12:06
  • last line where I am applying the dfApply function Sep 3, 2015 at 12:08
  • language is a column which referes to which language the text is in Sep 3, 2015 at 12:10
  • Why not just convert the whole column to English, then use a mask of non-english rows to replace only the ones you need to? That is a bit easier than using apply with your conditionals happening in each step. Sep 3, 2015 at 12:18
  • The language translator goslate is throwing timeout error for large number of data points Sep 3, 2015 at 12:29

1 Answer 1

0

Something like this might be an easier approach.

not_en = data["language"] != "en"
trans = translate(data[config['row']], "en")
col = config['row']
data.loc[not_en, col] = trans[not_en]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.