【python】pandas库pd.to_excel操作写入excel文件参数整理与实例
2018-01-18 16:58
3931 查看
excel的写入函数为pd.DataFrame.to_excel();必须是DataFrame写入excel, 即Write DataFrame to an excel sheet。
常用参数解析
excel_writer : ExcelWriter目标路径
sheet_name :excel表名命名
na_rep : 缺失值填充 ,可以设置为字符串
columns :选择输出的的列存入。
header :指定作为列名的行,默认0,即取第一行,数据为列名行以下的数据;若数据不含列名,则设定 header = None;
index:默认为True,显示index,当index=False 则不显示行索引(名字)
index_label:设置索引列的列名
to_excel(self, 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)
常用参数解析
excel_writer : ExcelWriter目标路径
In [16]: df = pd.read_csv('test.csv') In [17]: df Out[17]: index a_name b_name 0 0 1 3 1 1 2 3 2 2 3 4 #excel_writer :'excel_output.xls'输出路径 In [18]: df.to_excel('excel_output.xls')
sheet_name :excel表名命名
#得到的表名就是'biubiu' In [ 4000 20]: df.to_excel('excel_output.xls',sheet_name='biubiu')
na_rep : 缺失值填充 ,可以设置为字符串
In [25]: df = pd.read_excel('excel_output.xls') In [26]: df Out[26]: index a_name b_name 0 0 1 3.0 1 1 2 3.0 2 2 3 NaN #如果na_rep设置为bool值,则写入excel时改为0和1;也可以写入字符串或数字 In [27]: df.to_excel('excel_output.xls',na_rep=True) In [28]: pd.read_excel('excel_output.xls') Out[28]: index a_name b_name 0 0 1 3 1 1 2 3 2 2 3 1 In [29]: df.to_excel('excel_output.xls',na_rep=False) In [30]: pd.read_excel('excel_output.xls') Out[30]: index a_name b_name 0 0 1 3 1 1 2 3 2 2 3 0 In [31]: df.to_excel('excel_output.xls',na_rep=11) In [32]: pd.read_excel('excel_output.xls') Out[32]: index a_name b_name 0 0 1 3 1 1 2 3 2 2 3 11
columns :选择输出的的列存入。
In [44]: df.to_excel('excel_output.xls',na_rep=11,columns=['index']) In [45]: pd.read_excel('excel_output.xls') Out[45]: index 0 0 1 1 2 2
header :指定作为列名的行,默认0,即取第一行,数据为列名行以下的数据;若数据不含列名,则设定 header = None;
In [48]: df.to_excel('excel_output.xls',na_rep=11,index=False) In [49]: pd.read_excel('excel_output.xls') Out[49]: index a_name b_name 0 0 1 3 1 1 2 3 2 2 3 11 In [50]: df.to_excel('excel_output.xls',na_rep=11,index=False,header=None) In [51]: pd.read_excel('excel_output.xls') Out[51]: 0 1 3 0 1 2 3 1 2 3 11
index:默认为True,显示index,当index=False 则不显示行索引(名字)
index_label:设置索引列的列名
相关文章推荐
- 【python】pandas库pd.read_excel操作读取excel文件参数整理与实例
- 【python】pandas库pd.read_csv操作读取分隔符csv文件和文本text文件参数整理与实例
- Python对excel写入数据操作实例代码(只供参考)
- 使用Python和xlwt向Excel文件中写入中文的实例
- Python对excel写入数据操作实例代码(改进无法合并最后一组数据的问题)
- 【python】解析Excel中使用xlrd库、xlwt库操作,写入Excel文件详解(二)
- 【python】解析Excel中使用xlrd库、xlwt库操作,写入excel进阶完整脚本实例(五)
- Python对excel写入数据操作实例代码(只供参考)
- Python对excel写入数据操作实例代码(只供参考)
- Python对excel写入数据操作实例代码(只供参考)
- python操作excel文件并输出txt文件的实例
- python3 操作excel 写入xlsx类型文件 openpyxl
- Python对excel写入数据操作实例代码(改进无法合并最后一组数据的问题)
- Python对excel写入数据操作实例代码(改进无法合并最后一组数据的问题)
- Python对excel写入数据操作实例代码(改进无法合并最后一组数据的问题)
- 【整理】Python中,添加写入数据到已经存在的Excel的xls文件,即打开excel文件,写入新数据
- python文件和目录操作方法大全(含实例)
- python 文件操作API整理
- python文件操作整理汇总
- python文件和目录操作方法大全(含实例)