写出“for"的输出循环在 PYTHON 中表现出色 [英] Write the output from "for" loop to excel in PYTHON

查看:47
本文介绍了写出“for"的输出循环在 PYTHON 中表现出色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下一段代码:

my_list = ["US", "IT", "ES", "NL"]
for i in my_list:
    A = sum_products_by_country(world_level,i)
    df = pd.DataFrame({'value':A})
    Descending = df.sort_values( by='value', ascending = 0 )
    Top_5 = Descending[0:5]
    print(Top_5)

sum_products_by_country"是一个创建的函数,它将数据框(在我的例子中被命名为world_level")和一个国家/地区名称作为参数,并返回该国家/地区的乘积总和.使用这个循环,我找到了 my_list 中每个国家的 top5 产品和总和.这是这个循环的输出:

The "sum_products_by_country" is a created function which takes as arguments a data frame ( in my case is named "world_level") and a country name and returns the sum by product for this country. Using this loop I find the top5 products and the sums for each country of my_list. Here is the output of this loop:

US          value
Product  


B          1492

H          455

BB         351

C          119

F          117

IT          value
Product


P           346
U           331

A           379

Q           190

D          1389

ES         value
Product 


P          3046

U3         331

A          379

Q          1390

DD         10389

NL         value
Product 


P          3465

U          3313

AA         379

2Q         190

D          189

我想使用 :

writer = pd.ExcelWriter('top products.xlsx', engine='xlsxwriter')
Top_5.to_excel(writer, sheet_name='Sheet1')
writer.save()

你能告诉我应该把上面的代码放在哪里才能得到所需的excel文档吗?是否还有一种方法可以在我的 excel 文档的顶部只获取一次列名(国家、产品、值),而不是分别针对每个国家?所以我想要这样的东西:

Could you tell me where should I put the code above in order to get the required excel document? Is there also a way to get the column names(country,product,value) only once on the top in my excel document and not for each country separately? So I want something like this:

 Country   Product   value

  US        
           B          1492
           H          455
           BB         351
           C          119
           F          117

  IT          

           P           346
           U           331
           A           379
           Q           190
           D          1389

  ES         

          P          3046
          U3         331
          A          379
          Q          1390
          DD         10389

  NL         

          P          3465
          U          3313
          AA         379
          2Q         190
          D          189

谢谢

推荐答案

这个脚本应该可以帮助你:

This script should help you:

#Create workbook object
wb = openpyxl.Workbook()
sheet = wb.get_active_sheet()
sheet.title='Products by country'

#Generate data

#Add titles in the first row of each column
sheet.cell(row=1, column=1).value='country'
sheet.cell(row=1, column=2).value='product'
sheet.cell(row=1, column=3).value='value'


#Loop to set the value of each cell
for i in range(0, len(Country)):
sheet.cell(row=i+2, column=1).value=Country[i]#with country being your array full of country names. If you have 5 values for one country I would advise just having the country name in there five times.
sheet.cell(row=i+2, column=2).value=Product[i]#array with products
sheet.cell(row=i+2, column=3).value=Values[i]#array with values

#Finally, save the file and give it a name
wb.save('NameFile.xlsx')

这篇关于写出“for"的输出循环在 PYTHON 中表现出色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆