Python使用Pandas和XlsxWriter |S–1

Python Pandas是一个数据分析库。它可以读取, 过滤和重新排列大小数据集, 并以包括Excel在内的多种格式输出它们。
大熊猫使用XlsxWriter模块写入Excel文件。
XlsxWriter是一个Python模块, 用于在XLSX文件格式。它可以用于将文本, 数字和公式写入多个工作表。此外, 它还支持格式设置, 图像, 图表, 页面设置, 自动过滤器, 条件格式设置等功能。
代码1:使用Pandas和XlsxWriter将Pandas数据帧转换为xlsx文件。

# import pandas as pd import pandas as pd# Create a Pandas dataframe from some data. df = pd.DataFrame({ 'Data' : [ 'Geeks' , 'For' , 'geeks' , 'is' , 'portal' , 'for' , 'geeks' ]})# Create a Pandas Excel writer # object using XlsxWriter as the engine. writer = pd.ExcelWriter( 'pandasEx.xlsx' , engine = 'xlsxwriter' )# Write a dataframe to the worksheet. df.to_excel(writer, sheet_name = 'Sheet1' )# Close the Pandas Excel writer # object and output the Excel file. writer.save()

输出:
Python使用Pandas和XlsxWriter |S–1

文章图片
代码2:使用Pandas和XlsxWriter将多个数据框写入工作表。
# import pandas as pd import pandas as pd# Create some Pandas dataframes from some data. df1 = pd.DataFrame({ 'Data' : [ 11 , 12 , 13 , 14 ]}) df2 = pd.DataFrame({ 'Data' : [ 21 , 22 , 23 , 24 ]}) df3 = pd.DataFrame({ 'Data' : [ 31 , 32 , 33 , 34 ]})# Create a Pandas Excel writer object # using XlsxWriter as the engine. writer = pd.ExcelWriter( 'pandas_multiple.xlsx' , engine = 'xlsxwriter' )# Write each dataframe to a different worksheet. df1.to_excel(writer, sheet_name = 'Sheet1' ) df2.to_excel(writer, sheet_name = 'Sheet2' ) df3.to_excel(writer, sheet_name = 'Sheet3' )# Close the Pandas Excel writer object # and output the Excel file. writer.save()

输出:
Python使用Pandas和XlsxWriter |S–1

文章图片
Python使用Pandas和XlsxWriter |S–1

文章图片
Python使用Pandas和XlsxWriter |S–1

文章图片
代码3:
使用Pandas和XlsxWriter在工作表中定位数据框。
# import pandas as pd import pandas as pd# Create some Pandas dataframes from some data. df1 = pd.DataFrame({ 'Data' : [ 11 , 12 , 13 , 14 ]}) df2 = pd.DataFrame({ 'Data' : [ 21 , 22 , 23 , 24 ]}) df3 = pd.DataFrame({ 'Data' : [ 31 , 32 , 33 , 34 ]}) df4 = pd.DataFrame({ 'Data' : [ 41 , 42 , 43 , 44 ]})# Create a Pandas Excel writer object # using XlsxWriter as the engine. writer = pd.ExcelWriter( 'pandas_positioning.xlsx' , engine = 'xlsxwriter' )# write and Positioning the dataframes in the worksheet. # Default position, cell A1. df1.to_excel(writer, sheet_name = 'Sheet1' ) df2.to_excel(writer, sheet_name = 'Sheet1' , startcol = 3 ) df3.to_excel(writer, sheet_name = 'Sheet1' , startrow = 6 )# It is also possible to write the # dataframe without the header and index. df4.to_excel(writer, sheet_name = 'Sheet1' , startrow = 7 , startcol = 4 , header = False , index = False )# Close the Pandas Excel writer object # and output the Excel file. writer.save()

输出:
Python使用Pandas和XlsxWriter |S–1

文章图片
注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。
【Python使用Pandas和XlsxWriter |S–1】首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

    推荐阅读