pandas 写Excel 时报错:ValueError: Writing 0 cols but got 8 aliases

pandas 写Excel 时报错:

Traceback (most recent call last):
  File "D:\code_tool\spiderYesmro3\main.py", line 244, in <module>
    main()
  File "D:\code_tool\spiderYesmro3\main.py", line 33, in main
    df.to_excel(writer, sheet_name=cate, header=excel_title, index=False)
  File "D:\code_tool\spiderYesmro3\venv\lib\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "D:\code_tool\spiderYesmro3\venv\lib\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "D:\code_tool\spiderYesmro3\venv\lib\site-packages\pandas\core\generic.py", line 2374, in to_excel
    formatter.write(
  File "D:\code_tool\spiderYesmro3\venv\lib\site-packages\pandas\io\formats\excel.py", line 950, in write
    writer._write_cells(
  File "D:\code_tool\spiderYesmro3\venv\lib\site-packages\pandas\io\excel\_openpyxl.py", line 492, in _write_cells
    for cell in cells:
  File "D:\code_tool\spiderYesmro3\venv\lib\site-packages\pandas\io\formats\excel.py", line 887, in get_formatted_cells
    for cell in itertools.chain(self._format_header(), self._format_body()):
  File "D:\code_tool\spiderYesmro3\venv\lib\site-packages\pandas\io\formats\excel.py", line 691, in _format_header_regular
    raise ValueError(
ValueError: Writing 0 cols but got 8 aliases

复现:

总结:

原因:DataFrame 中的数据源为空导致

posted @   chuangzhou  阅读(693)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2022-02-01 Python - 函数中的类型提示
2022-02-01 Python - 模块内省
点击右上角即可分享
微信分享提示