pandas style.bar() to_excel
时间: 2023-09-20 19:12:47 浏览: 150
你可以使用 to_excel()
方法将 Pandas 数据框中的带有条形图的样式保存到 Excel 文件中。以下是一个示例代码:
import pandas as pd
# 创建一个示例数据框
students = pd.DataFrame({
'Name': ['John', 'Mary', 'Peter', 'Linda'],
'Test_1': [80, 90, 70, 85],
'Test_2': [75, 85, 80, 90],
'Test_3': [90, 80, 85, 75]
})
# 使用 style.bar() 方法进行条形图着色
styled_students = students.style.bar(color='orange',subset=['Test_1','Test_2','Test_3'])
# 将带有样式的数据框保存到 Excel 文件中
with pd.ExcelWriter('students.xlsx', engine='xlsxwriter') as writer:
styled_students.to_excel(writer, sheet_name='Sheet1', index=False)
这个例子使用 style.bar()
方法创建了一个带有条形图的样式数据框,然后将其保存到名为 students.xlsx
的 Excel 文件中。使用 Pandas 的 ExcelWriter()
方法创建一个写入器对象,并将其传递给 to_excel()
方法,以便将数据框保存到 Excel 文件中。请注意,index=False
参数用于指定不在 Excel 文件中包含行索引。
相关推荐

















