[英]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.