简体   繁体   English

如何将 dfs 导出到带有多个工作表和不同工作表名称的 excel 熊猫

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

相关问题 使用pandas在excel中创建多个工作表以循环工作表名称 - Create multiple sheets in excel using pandas to loop through sheet names 熊猫读取具有多个工作表和不同页眉偏移量的Excel工作表 - pandas read excel sheet with multiple sheets and different header offsets 在pandas中使用不同的工作表名称读取多个excel文件 - Read multiple excel file with different sheets names in pandas Pandas to_excel() - 无法弄清楚如何使用 for 循环将多个数据帧导出为一个 excel 中的不同工作表 - Pandas to_excel() - can't figure out how to export multiple dataframes as different sheets in one excel using for loop 将熊猫数据框导出到Excel工作表中的不同行 - Export pandas dataframes to different rows in Excel sheet 通过python pandas将3个SQL查询结果导出到excel表中的3个表中 - Export 3 SQL Query results to 3 sheets in an excel sheet through python pandas 如何通过工作表名称(Pandas)解析Excel工作表 - How to parse excel sheets by sheet name (Pandas) 如何合并多个工作表并用工作表名称重命名列名? - How to merge multiple sheets and rename column names with the names of the sheet names? 当工作表有多个可能的名称时,如何将多个 Excel 工作表读入 python - How to read in multiple excel sheets into python when there is multiple possible names for a sheet 使用模式中的不同工作表名称读取熊猫中的Excel工作表 - Read excel sheet in pandas with different sheet names in a pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM