pandas 将dataframe数据写入Excel中

写入 Excel 操作警告

使用下面的代码将数据写入 Excel 结果出现警告

df.to_excel(output_name,index=False)

43: FutureWarning: As the xlwt package is no longer maintained, the xlwt engine will be removed in a future version of pandas. This is the only engine in pandas that supports writing in the xls format. Install openpyxl and write to an xlsx file instead. You can set the option io.excel.xls.writer to ‘xlwt’ to silence this warning. While this option is deprecated and will also raise a warning, it can be globally set and the warning suppressed.

官网文档

官方解释

解决

改用 openpyxl 库读取/写入 Excel 2010 xlsx/xlsm/xltx/xltm 文件

pip install openpyxl

工作簿是文档所有其他部分的容器

# 从 openpyxl 模块,导入Workbook类
from openpyxl import Workbook
book = Workbook()

始终使用至少一个工作表创建一个工作簿

# 创建一个新的工作簿
sheet = book.active

对 dataframe 类型数据的添加操作

# 使用dataframe_to_rows方法将数据添加到表中
from openpyxl.utils.dataframe import dataframe_to_rows
for row in dataframe_to_rows(data, index=False, header=True):
    sheet.append(row)

使用 save() 方法将内容写入 sample.xlsx 文件

# 写入内容
book.save("sample.xlsx")

相关链接:

  1. Pandas:Options and settings
  2. Openpyxl 教程
  3. Python读写EXCEL文件常用方法大全
  4. import os总结
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值