简体   繁体   English

使用 Python/Pandas 创建具有多张工作表的 Excel 文件

[英]Create an Excel file with multiple sheets with Python/Pandas

I have a big dataset (df) (8M rows, 50 columns).我有一个大数据集(df)(8M 行,50 列)。 I am trying to build a for loop to create an excel file where each sheet holds the value_counts() of each of the column of the dataset.我正在尝试构建一个 for 循环来创建一个 excel 文件,其中每个工作表都包含数据集每个列的 value_counts()。

(ie on worksheet('Sheet1') I write df.columns[0].value_counts() and on worksheet('Sheet2') I write df.columns[1].value_counts() etc etc). (即在 worksheet('Sheet1') 我写 df.columns[0].value_counts() 和在 worksheet('Sheet2') 我写 df.columns[1].value_counts() 等等)。

Here's what I tried:这是我尝试过的:

for i in range(3,6):   # I am using a small range to test the loop
    z = df1[df1.columns[i]].value_counts()
    z = z.to_frame().reset_index()
    title = str(i)
    with pd.ExcelWriter('Pivot part1.xlsx') as writer:  
        z.to_excel(writer, sheet_name=title)

This keeps overwrite the file so that I ended up with an excel file with only one sheet rather the an Excel file with 4 sheets.这会一直覆盖文件,因此我最终得到了一个只有一张纸的 excel 文件,而不是一个有 4 张纸的 Excel 文件。

I hope I managed to explain clearly the issue and I apologize if this question is a duplicate, but I couldn't find a suitable answer, or at least one I could understand.我希望我设法清楚地解释了这个问题,如果这个问题是重复的,我深表歉意,但我找不到合适的答案,或者至少找不到一个我能理解的答案。

Re-arrange so that you only open the excel writer object once:重新安排,使您只打开 excel 写入器 object 一次:

with pd.ExcelWriter('Pivot part1.xlsx') as writer:  
    for i in range(3,6):
        z = df1[df1.columns[i]].value_counts()
        z = z.to_frame().reset_index()
        title = str(i)
        z.to_excel(writer, sheet_name=title)

You create the writer object once and it shall all work fine.您创建编写器 object 一次,它应该可以正常工作。

writer = pd.ExcelWriter('Pivot part1.xlsx', engine='xlsxwriter')
for i in range(3,6):   # I am using a small range to test the loop
    z = df1[df1.columns[i]].value_counts()
    z = z.to_frame().reset_index()
    title = str(i)
    z.to_excel(writer, sheet_name=title)
writer.save()

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

相关问题 如何在 pandas 中打开一个 excel 文件? - How to open an excel file with multiple sheets in pandas? 如何从 python 读取多张 excel 文件? 我收到错误消息说“pandas”没有属性“excel” - How to read excel file with multiple sheets from python? I got error saying 'pandas' has no attribute 'excel' 如何通过Python创建多个excel表? - How to create multiple excel sheets through Python? python pandas中的多个数据框到单个excel文件(两个不同的工作表) - Multiple data frame to single excel file(two different sheets) in python pandas 使用pandas在excel中创建多个工作表以循环工作表名称 - Create multiple sheets in excel using pandas to loop through sheet names Python / Pandas:使用“for-loop”将多个Dataframes写入Excel工作表 - Python/Pandas: writing multiple Dataframes to Excel sheets using a “for-loop” 使用Python中的pandas在现有excel中添加多个工作表 - Add multiple sheets in existing excel using pandas in Python "使用数据框 Python pandas 创建多个 Excel 工作表" - Creating Multiple Excel sheets using data frames Python pandas Python:在多个工作表上将pandas DataFrame写入Excel的最快方法 - Python: fastest way to write pandas DataFrame to Excel on multiple sheets 使用python pandas在excel中的多个工作表中写入数据 - Write data in multiple sheets in excel using python pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM