将pandas写入excel的次数限制在每张纸上100万行

2024-10-04 09:18:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大约有2800万行(5列)的数据框架,我正在努力将其写入excel,它限制在1048576行,我不能在多个工作簿中使用它,所以我需要将这些28Mi拆分为28个工作表,以此类推

这就是我用它所做的:

writer = pd.ExcelWriter('NAME_XX-' + mes +'-' + ano + '_XXX.xlsx', engine = "xlsxwriter")

notMor.to_excel(writer, engine='xlsxwriter', index=False)

得到这个错误: enter image description here

我曾想过将数据框拆分成27个不同的数据框,然后将每个数据框保存在工作簿上,但没有更简单的方法吗


Tags: 数据name框架xlsxexcelenginexxxwriter
2条回答

简单/简单/快速>;将您的数据从Pandas下载到拆分1000000的CSV,然后让Excel打开它。Excel将相应地调整并打开多个工作表

您可以调整一些代码:

#split file code
lines_per_file = 1000000
smallfile = None
with open('Plan.txt') as bigfile:
    for lineno, line in enumerate(bigfile):
        if lineno % lines_per_file == 0:
            if smallfile:
                smallfile.close()
            small_filename = 'small_file_{}.txt'.format(lineno + lines_per_file)
            smallfile = open(small_filename, "w")
        smallfile.write(line)
    if smallfile:
        smallfile.close()
  1. 在Excel 2016和Excel for Microsoft 365中,使用数据Get&;转换数据>获取数据从任意数量的外部数据源导入数据,例如文本文件、Excel工作簿、网站、Microsoft Access、SQL Server或其他包含多个相关表的关系数据库。
    • 在Excel 2013和2010中,转到电源查询获取外部数据,然后选择数据源
  2. Excel提示您选择一个表。如果要从同一数据源获取多个表,请选中启用多表选择选项。选择多个表时,Excel会自动为您创建数据模型
  3. 选择一个或多个表,然后单击加载

enter image description here

  • 此示例有3M行

enter image description here

相关问题 更多 >