栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pandas 将dataframe数据写入Excel中

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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总结
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/341416.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号