Home > Blockchain >  Iterate over dataframe and adding rows to new dataframe
Iterate over dataframe and adding rows to new dataframe

Time:03-24

I have following table which I have in data frame

sku Description
91567 Hole

What I need is translate every Description for given SKu into several languages and place them into new dataframe, so that I am able to export it in excel.

I was able to do it for 1 sku if values are in lists, but with dataframe I am completely lost. Plus it doesnt work for multiple skus and descriptions as decription is always linked to specific sku


import pandas as pd
from deep_translator import GoogleTranslator
#from deep_translator import DeepL
#print(DeepL(source='auto', target='cs').translate(text='happy coding'))
#import  deep_translator as dt

lang=['cs','fr','nl','de','hu','pl','pt','ro','sk','es']

term=['Hole']
sku=['91567']

lst = []
cols = ['sku','original', 'translation','lang']
for s in sku:
   for t in term:
       for l in lang:
           m= GoogleTranslator(source='auto', target=l).translate(text=t)
           lst.append([s,t,m,l])

df1 = pd.DataFrame(lst, columns=cols)

   pd.get_option('display.max_columns')
pd.set_option('display.max_colwidth', -1) 
df1.to_csv('mycsvfile.csv',index=False,encoding='utf-8-sig')
df1

My inteded result is to have new dataframe with sku, description original, language to which it was translated, translation. That means = sku and original description will be there as many times as many languages we have in the lang list.

Please, could you help me?

CodePudding user response:

"it doesnt work for multiple skus and descriptions as decription is always linked to specific sku". Indeed in your example, you have a nested for, looping first over sku, and then over term.

for n, s in enumerate(sku):
   for l in lang:
       m = GoogleTranslator(source='auto', target=l).translate(text=term[n])
       lst.append([s,term[n],m,l])

should fix it.

Considering pandas, you can try something like the following but I strongly encourage you to use a debugger to understand each step during the manipulation of df and to look for each function you didn't know in the pandas doc :

def translate(term, lang):
    return GoogleTranslator(source='auto', target=lang).translate(text=term)

term=['Hole','Box']
sku=['91567','58897']
lang=['cs','fr','nl','de','hu','pl','pt','ro','sk','es']

df = pd.DataFrame({
    'Description' : term,
    'sku' : sku
})

df.loc[:, 'lang']=df.Description.apply(lambda x : lang)
df = df.explode("lang", ignore_index=True)
df.loc[:, "translation"] = df.loc[:, ["Description" , "lang"]].apply(lambda x : translate(*x), axis=1)

df
Out[111]: 
   Description    sku lang translation
0         Hole  91567   cs       Otvor
1         Hole  91567   fr        Trou
2         Hole  91567   nl         Gat
3         Hole  91567   de        Loch
4         Hole  91567   hu        Lyuk
5         Hole  91567   pl       Otwór
6         Hole  91567   pt    Orifício
7         Hole  91567   ro       Gaură
8         Hole  91567   sk       Diera
9         Hole  91567   es     Agujero
10         Box  58897   cs         Box
11         Box  58897   fr       Boîte
12         Box  58897   nl        Doos
13         Box  58897   de      Kasten
14         Box  58897   hu       Doboz
15         Box  58897   pl    Skrzynka
16         Box  58897   pt       Caixa
17         Box  58897   ro       Cutie
18         Box  58897   sk         Box
19         Box  58897   es        Caja
  • Related