写入 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")
相关链接: