Python | 使用 Pandas 和 XlsxWriter | 集合 – 3
Python Pandas 是一个数据分析库。它可以读取、过滤和重新排列小型和大型数据集,并以包括 Excel 在内的多种格式输出它们。
Pandas使用 XlsxWriter 模块写入 Excel 文件。
XlsxWriter是一个用于以 XLSX 文件格式写入文件的 Python 模块。它可用于将文本、数字和公式写入多个工作表。此外,它还支持格式化、图像、图表、页面设置、自动筛选、条件格式等功能。
代码#1:使用 Pandas 和 XlsxWriter 绘制柱形图。
# import pandas library as pd import pandas as pd
# Create a Pandas dataframe from some data. dataframe = pd.DataFrame({ 'Subject': ["Math", "Physics", "Computer", "Hindi", "English", "chemistry"], 'Mid Exam Score' : [90, 78, 60, 80, 60, 90], 'End Exam Score' : [45, 39, 30, 40, 30, 60] })
# Create a Pandas Excel writer # object using XlsxWriter as the engine. writer_object = pd.ExcelWriter('pandas_column_chart.xlsx', engine ='xlsxwriter')
# Write a dataframe to the worksheet. dataframe.to_excel(writer_object, sheet_name ='Sheet1')
# Create xlsxwriter workbook object . workbook_object = writer_object.book
# Create xlsxwriter worksheet object worksheet_object = writer_object.sheets['Sheet1']
# set width of the B and C column worksheet_object.set_column('B:C', 20)
# Create a chart object that can be added # to a worksheet using add_chart() method.
# here we create a column chart object . chart_object = workbook_object.add_chart({'type': 'column'})
# Add a data series to a chart # using add_series method.
# Configure the first series. # synt |