首页 > 行业资讯 > Pandas 教程-Pandas DataFrame.to_excel()

Pandas 教程-Pandas DataFrame.to_excel()

时间:2024-04-04 来源: 浏览:

Pandas 教程-Pandas DataFrame.to_excel()

点击关注 Python架构师
Python架构师

gh_1d7504e4dee1

回复:python,领取Python面试题。分享Python教程,Python架构师教程,Python爬虫,Python编程视频,Python脚本,Pycharm教程,Python微服务架构,Python分布式架构,Pycharm注册码。

整理: python架构师

我们可以使用to_excel()函数将DataFrame导出到Excel文件中。

要将单个对象写入Excel文件,我们必须指定目标文件名。如果要写入多个工作表,我们需要创建一个ExcelWriter对象,其中包含目标文件名,并且还需要指定要在其中写入的文件中的工作表。

也可以通过指定唯一的sheet_name来写入多个工作表。必须保存对文件写入的所有数据的更改。

注意:如果我们使用已经存在的文件名创建了一个ExcelWriter对象,它将擦除现有文件的内容。

语法

DataFrame.to_excel(excel_writer, sheet_name= ’Sheet1’ , na_rep= ’’ , float_format= None , columns= None , header= True , index= True , index_label= None , startrow= 0 , startcol= 0 , engine= None , merge_cells= True , encoding= None , inf_rep= ’inf’ , verbose= True , freeze_panes= None )  

资源分享

点击领取:最全Python资料合集

参数

  • excel_writer :文件路径或现有的ExcelWriter。

  • sheet_name :指的是包含DataFrame的工作表的名称。

  • na_repr :缺失数据的表示。

  • float_format :这是一个可选参数,用于格式化浮点数的字符串。

  • columns :指定要写入的列。

  • header :写出列名。如果给定字符串的列表,则假定它是列名的别名。

  • index :写入索引。

  • index_label :指定索引列的列标签。如果未指定,并且标题和索引为True,则使用索引名称。如果DataFrame使用MultiIndex,则应给出一个序列。

  • startrow :默认值为0。它指的是要将DataFrame倾倒到的左上角单元格行。

  • startcol :默认值为0。它指的是要将DataFrame倾倒到的左上角单元格列。

  • engine :这是一个可选参数,用于指定要使用的引擎,可以是openpyxl或xlsxwriter。

  • merge_cells :它返回布尔值,其默认值为True。将MultiIndex和分层行写为合并单元格。

  • encoding :这是一个可选参数,用于对生成的excel文件进行编码。仅对xlwt是必需的。

  • inf_rep :这也是一个可选参数,默认值为inf。通常表示无穷大。

  • verbose :它返回一个布尔值。默认值为True。用于在错误日志中显示更多信息。

  • freeze_panes :这也是一个可选参数,用于指定要冻结的基于一的最底行和最右列。

示例

import pandas as pd # create dataframe info_marks = pd.DataFrame({ ’name’ : [ ’Parker’ , ’Smith’ , ’William’ , ’Terry’ ], ’Maths’ : [ 78 , 84 , 67 , 72 ], ’Science’ : [ 89 , 92 , 61 , 77 ], ’English’ : [ 72 , 75 , 64 , 82 ]}) # render dataframe as html writer = pd.ExcelWriter( ’output.xlsx’ ) info_marks.to_excel(writer) writer.save() print( ’DataFrame is written successfully to the Excel File.’ )

输出

DataFrame已成功写入Excel文件

 
热门推荐
  • 离谱!X为员工请假一天,代价是3700
  • Pandas 教程-Pandas DataFrame.sum()
  • 只有前端崩盘?后端和测试还行?网友:都一样~

下一条:返回列表
版权:如无特殊注明,文章转载自网络,侵权请联系cnmhg168#163.com删除!文件均为网友上传,仅供研究和学习使用,务必24小时内删除。
相关推荐