如何使用for循环将数据框导出到Excel工作簿作为工作表。

huangapple go评论69阅读模式
英文:

How to Export Dataframes to Excel Workbook as Sheets with For Loop

问题

I want to export this data to a single workbook rather than multiple workbooks.

我想将这些数据导出到一个工作簿,而不是多个工作簿。

英文:

I've written a code that runs through a for loop to generate data into dataframes but I want to export this data to a single workbook rather than multiple workbooks (actual code is on work computer and work stuff is confidential so dummy code is below)

Below is a general idea of how the code runs without the actual details of how the dataframes are composed. (Dummy code is running on csv files I pulled off Yahoo Finance)

FileName_List = ["AAPL", "GME", "MSFT", "NTDOY", "Z"]

for FileName in FileName_List:
    ReadName = FileName + ".csv"
    Raw_Data = pd.read_csv(ReadName, sep = ",")
  
    # removed code that actually manipulates data as it's not needed for example purposes and alludes to confidential data

    Raw_Data.to_excel("Final_Output.xlsx", sheet_name = FileName)

So now my directory has a bunch of Excel. I could manually just move them all into a single work book but with the actual amount of files I'll be running through, it would be just a time waste to do that.

I would want the end of my code to run everything to the same workbook and each sheet named with the for loop

The files land as is in separate files, so I can't change how the files hit. Again the above sample code is not actually the real number of files this code will have to run though

I've also tried using ExcelWriter but after the first loop it errors ask asking for a zipfile format using example code I found here: https://www.statology.org/write-multiple-excel-sheets-pandas/

答案1

得分: 1

The DataFrame.to_excel() 函数在想要在工作簿中创建多个工作表时如下所示:

> 如果您希望写入工作簿中的多个工作表,则需要指定一个 ExcelWriter 对象:
>
> df2 = df1.copy()
> with pd.ExcelWriter('output.xlsx') as writer:
> df1.to_excel(writer, sheet_name='Sheet_name_1')
> df2.to_excel(writer, sheet_name='Sheet_name_2')

因此,将上述代码应用到您的代码中,如下所示:

FileName_List = ["AAPL", "GME", "MSFT", "NTDOY", "Z"]
# 创建 ExcelWriter 对象
df_list = []
for FileName in FileName_List:
    ReadName = FileName + ".csv"
    Raw_Data = pd.read_csv(ReadName, sep = ",")

    # 假设这里有实际操作数据的代码
    # 将数据添加到 df 列表中
    df_list.append(Raw_Data)

with pd.ExcelWriter('Final_Output.xlsx') as writer:
    for FileName, df in zip(FileName_List, df_list):
        # 使用 writer 对象将数据写入 Excel 工作表
        df.to_excel(writer, sheet_name=FileName)

注意:您需要首先操作数据帧并将它们附加到列表中。完成后,您可以将它们一次性写入 Excel 工作簿作为多个工作表。

英文:

The DataFrame.to_excel() says the following when wanting to create more that one sheet in a workbook:

> If you wish to write to more than one sheet in the workbook, it is
> necessary to specify an ExcelWriter object:
>
> df2 = df1.copy()
> with pd.ExcelWriter('output.xlsx') as writer:
> df1.to_excel(writer, sheet_name='Sheet_name_1')
> df2.to_excel(writer, sheet_name='Sheet_name_2')

So taking the code mentioned above and applying it to your code it would like this:

FileName_List = ["AAPL", "GME", "MSFT", "NTDOY", "Z"]
# Create ExcelWriter object
df_list = []
for FileName in FileName_List:
    ReadName = FileName + ".csv"
    Raw_Data = pd.read_csv(ReadName, sep = ",")


    # PRETEND THAT THERE'S CODE HERE THAT ACTUALLY MANIPULATES THE DATA
    # Append to df list
    df_list.append(Raw_Data)

with pd.ExcelWriter('Final_Output.xlsx') as writer:
    for FileName, df in zip(FileName_List, df_list):
        # Write to excel sheet with writer object
        df.to_excel(writer, sheet_name=FileName)

Note: you want to manipulate your dataframes first and append them into a list. Once that is done, you want to write them into a excel workbook in one go as multiple worksheets.

huangapple
  • 本文由 发表于 2023年4月17日 06:57:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定