I've got the following data frame
df = pd.DataFrame(data= {'Product_JP': ['トマトコ- サルサ C225G','マトケチヤツプ','トマトケチヤツプバリユ-','ケチヤツプハ-フ','トマトケチヤツププレミアム'],
'Value1': [1,12313,1.123,0.112,0],
'Metric1_JP': ['マ-ケットサイズ(販売金額(x1000))','加重販売率(販売金額)','アイテム販売店当り(販売個数)','加重販売率(販売金額)','加重販売率(販売金額)'],
'Type_JP': ['サルサソ−ス','ケチャップ','ケチャップ','ケチャップ','ケチャップ'],
'SKU': [4582152498325,4582112498325,4500152498325,4582112398325,4582152483125]},
)
Product_JP Value1 Metric1_JP Type_JP SKU
0 トマトコ- サルサ C225G 1.000 マ-ケットサイズ(販売金額(x1000)) サルサソ−ス 4582152498325
1 マトケチヤツプ 12313.000 加重販売率(販売金額) ケチャップ 4582112498325
2 トマトケチヤツプバリユ- 1.123 アイテム販売店当り(販売個数) ケチャップ 4500152498325
3 ケチヤツプハ-フ 0.112 加重販売率(販売金額) ケチャップ 4582112398325
4 トマトケチヤツププレミアム 0.000 加重販売率(販売金額) ケチャップ 4582152483125
And I can apply the following function using df.apply()
from deep_translator import (GoogleTranslator)
df['Product_EN'] = df['Product_JP'].apply(lambda row:GoogleTranslator(source='ja', target='en').translate(row))
Product_JP Value1 Metric1_JP Type_JP SKU \
0 トマトコ- サルサ C225G 1.000 マ-ケットサイズ(販売金額(x1000)) サルサソ−ス 4582152498325
1 マトケチヤツプ 12313.000 加重販売率(販売金額) ケチャップ 4582112498325
2 トマトケチヤツプバリユ- 1.123 アイテム販売店当り(販売個数) ケチャップ 4500152498325
3 ケチヤツプハ-フ 0.112 加重販売率(販売金額) ケチャップ 4582112398325
4 トマトケチヤツププレミアム 0.000 加重販売率(販売金額) ケチャップ 4582152483125
Product_EN
0 Tomatoco-Salsa C225G
1 Matthew
2 Tomato miser
3 Catch
4 Tomato miser premium
But what I want to do is to pass a list of columns to apply in one go like so
JP_columns = [column for column in df.columns if '_JP' in column]
EN_columns = [column.replace('_JP', '_EN') for column in JP_columns]
df[EN_columns] = df[JP_columns].apply(lambda row:GoogleTranslator(source='ja', target='en').translate(row))
This returns a ValueError: "The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."
- What am I doing wrong with
df.apply() - Would this be better done using
np.vectorize?
for example (Also returns a Value Error: "The truth value of a DataFrame is ambiguous")
df[EN_columns] = np.vectorize(GoogleTranslator(source='ja', target='en').translate(df[JP_columns]))
Thanks