简体   繁体   English

迭代 dataframe 并将行添加到新的 dataframe

[英]Iterate over dataframe and adding rows to new dataframe

I have following table which I have in data frame我在数据框中有下表

sku存货 Description描述
91567 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.我需要的是将给定 SKu 的每个描述翻译成多种语言并将它们放入新的 dataframe,以便我能够将其导出到 excel。

I was able to do it for 1 sku if values are in lists, but with dataframe I am completely lost.如果值在列表中,我能够为 1 sku 做到这一点,但是对于 dataframe 我完全迷路了。 Plus it doesnt work for multiple skus and descriptions as decription is always linked to specific sku此外,它不适用于多个 SKU 和描述,因为描述始终链接到特定的 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.我的预期结果是有新的 dataframe 和 sku,原始描述,翻译成的语言,翻译。 That means = sku and original description will be there as many times as many languages we have in the lang list.这意味着 = sku 和原始描述的数量将是 lang 列表中的语言数量的两倍。

Please, could you help me?拜托,你能帮帮我吗?

"it doesnt work for multiple skus and descriptions as decription is always linked to specific sku". “它不适用于多个 SKU 和描述,因为描述总是链接到特定的 SKU”。 Indeed in your example, you have a nested for, looping first over sku, and then over term.实际上,在您的示例中,您有一个嵌套的 for,首先在 sku 上循环,然后在 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 :考虑到 pandas,您可以尝试以下操作,但我强烈建议您使用调试器来理解 df 操作过程中的每个步骤,并在 pandas 文档中查找您不知道的每个 function:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM