【问题标题】:Export List of Pandas Dataframes in Python在 Python 中导出 Pandas 数据框列表
【发布时间】:2016-07-06 15:52:39
【问题描述】:

我正在尝试将 pandas 数据框列表导出到 excel

list_of_df_to_dump = [df1,df2,...,df100]
list_of_tab_names = ['df1','df2',...,'df100']

writer = ExcelWriter(excel_name + '.xlsx')
for i,j in list_of_df_to_dump,list_of_tab_names:
     i.to_excel(writer,j,index = False)
writer.save()

我收到以下错误:

TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

关于如何解决此问题或完成相同事情的替代方法有什么想法吗?我不知道列表要手动执行多久

【问题讨论】:

  • 您的 for 语句不正确。试试看:for i,j in zip(list_of_df_to_dump,list_of_tab_names):
  • 很奇怪。编造一些数据来测试你的代码我得到了一个 ValueError ,正如我所料。请发帖minimal reproducible example

标签: python excel pandas xlsxwriter


【解决方案1】:

您必须使用zip 来遍历两个列表中的项目对。尝试以下修复:

list_of_df_to_dump = [df1,df2,...,df100]
list_of_tab_names = ['df1','df2',...,'df100']

writer = ExcelWriter(excel_name + '.xlsx')
for df, tab_name in zip(list_of_df_to_dump, list_of_tab_names):
     df.to_excel(writer, tab_name, index=False)
writer.save()

【讨论】:

    猜你喜欢
    • 2022-06-16
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 2014-10-20
    • 2014-10-15
    相关资源
    最近更新 更多