简体   繁体   English

Pandas:遍历 DataFrame 列表并将每个导出到 Excel 表

[英]Pandas: Iterate through a list of DataFrames and export each to excel sheets

Trying to teach myself coding to automate some tedious tasks at work.试图自学编码以自动化工作中的一些乏味任务。 I apologize for any unintentional ignorance.对于任何无意的无知,我深表歉意。

I have created Data Frames in pandas (python 3.x).我在熊猫(python 3.x)中创建了数据帧。 I want to print each data frame to a different excel sheet.我想将每个数据框打印到不同的 Excel 工作表。 Here is what I have for 2 Data Frames, it works perfect but I want to scale it to loop through a list of data frames so that I can make it a bit more dynamic.这是我拥有的 2 个数据帧,它工作得很好,但我想缩放它以循环遍历数据帧列表,以便我可以使它更具动态性。

writer = pandas.ExcelWriter("MyData.xlsx", engine='xlsxwriter')
Data.to_excel(writer, sheet_name="Data")
ByBrand.to_excel(writer, sheet_name="ByBrand")
writer.save()

Easy enough, but when there are 50+ sheets that need to be created it will get tedious.很容易,但是当需要创建 50 多张纸时,它会变得乏味。

Here is what I tried, it did not work:这是我尝试过的,没有用:

writer = pandas.ExcelWriter("MyData.xlsx", engine='xlsxwriter')
List = [Data , ByBrand]
for i in List:
        i.to_excel(writer, sheet_name= i)
writer.save()

I think the issue is that the sheet_name field must be a string because as-is it creates an error.我认为问题在于 sheet_name 字段必须是一个字符串,因为它会产生错误。 But if I put sheet_name= "i", it only creates one sheet called "i" with the data from Data, but doesn't iterate to ByBrand.但是,如果我放置 sheet_name="i",它只会使用来自 Data 的数据创建一个名为“i”的工作表,但不会迭代到 ByBrand。 Also, the excel file would be a nightmare if the sheets weren't named to their corresponding data frame, so please no suggestions for things like numbered sheets.此外,如果工作表没有以相应的数据框命名,那么 excel 文件将是一场噩梦,因此请不要对编号工作表之类的东西提出建议。

Thank you so much in advance, this website has been invaluable for my journey into coding.非常感谢您,这个网站对我的编码之旅非常宝贵。

-Stephen -斯蒂芬

It is easier to go from the string 'Data' to the value Data than the other way around.从字符串'Data'到值Data比反过来更容易。 You can use locals()['Data'] to access the value associated to the variable whose string name is 'Data' :您可以使用locals()['Data']访问与字符串名称为'Data'的变量关联的值:

import pandas as pd

writer = pd.ExcelWriter("MyData.xlsx", engine='xlsxwriter')
seq = ['Data', 'ByBrand']
for name in seq:
    df = locals()[name]
    df.to_excel(writer, sheet_name=name)
writer.save()

locals() returns a read-only dictionary containing the current scope's local variables. locals()返回一个只读字典,其中包含当前作用域的局部变量。 globals() returns a dictionary containing the current scope's global variables. globals()返回一个包含当前作用域的全局变量的字典。 (Thus, if Data and ByBrand are defined in the global namespace rather than the local namespace, use globals() instead of locals() .) (因此,如果DataByBrand是在全局命名空间而不是本地命名空间中定义的,请使用globals()而不是locals() 。)


Another option is to collect the DataFrames in a dict.另一种选择是在字典中收集数据帧。 Instead of creating a variable for each DataFrame, make one dict, and let the keys be the sheet names and the values be DataFrames:不是为每个 DataFrame 创建一个变量,而是创建一个 dict,让键为工作表名称,值为 DataFrames:

import pandas as pd

dfs = dict()
dfs['Data'] = ...
dfs['ByBrand'] = ...

writer = pd.ExcelWriter("MyData.xlsx", engine='xlsxwriter')
for name, df in dfs.items():
    df.to_excel(writer, sheet_name=name)
writer.save()

I think this is preferable since it does not require introspection tools like locals() or globals() .我认为这是可取的,因为它不需要像locals()globals()这样的内省工具。 This second approach just uses a dict the way dicts are intended to be used: mapping keys to values.第二种方法只是使用字典的方式来使用字典:将键映射到值。

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

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