[英]How to export dfs to excel with multiple sheets and different sheet names pandas
I am trying to iterate through student ID's WITH A LOOP by printing a df to a sheet in excel and having the student ID be the tab name.我试图通过将 df 打印到 excel 中的工作表并将学生 ID 作为选项卡名称来遍历学生 ID 的 WITH A LOOP。
In other words:换句话说:
df to excel and tab name is 1 df 到 excel,选项卡名称为 1
next df to new sheet and tab name is 2新工作表和选项卡名称的下一个 df 是 2
iterate through all student IDs遍历所有学生 ID
final=[dataframe,dataframe,dataframe]
studentIDs=[1,2,3]
writer = pd.ExcelWriter('Name.xlsx', engine='xlsxwriter')
for df in final:
df.to_excel(writer, sheet_name='%s' %studentIDs)
writer.save()
final=[dataframe,dataframe,dataframe]
studentIDs=[1,2,3]
writer = pd.ExcelWriter('Name.xlsx', engine='xlsxwriter')
for i,df in enumerate(final, 0):
df.to_excel(writer, sheet_name='%s' %studentIDs[i])
writer.save()
If the lists will always match in order, then you can use enumerate (which gives you a list starting from that index onward) and then match it up to the list above, but I would recommend using a dictionary如果列表总是按顺序匹配,那么您可以使用 enumerate (它为您提供一个从该索引开始的列表),然后将其与上面的列表匹配,但我建议使用字典
final = {
1 : dataframe,
2 : dataframe,
3 : dataframe
}
writer = pd.ExcelWriter('Name.xlsx', engine='xlsxwriter')
for sheet_name in final:
final[sheet_name].to_excel(writer, sheet_name= str(sheet_name))
writer.save()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.