Reputation: 317
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')
Upvotes: 0
Views: 2282
Reputation: 829
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]
Upvotes: 0