gpt4 book ai didi

python - pandas.to_csv() 一些单词为粗体

转载 作者:行者123 更新时间:2023-12-04 02:56:45 36 4
gpt4 key购买 nike

我有数据框

df = [["A1" "B2" "C3"] ["D4" "E5" "F6"]]

(所有文本均为字符串)我想使用 df.to_csv() 将其保存到一个文件中,文本“E5”为彩色/粗体。可以吗?

最佳答案

逗号分隔值 (csv) 文件不包含任何格式信息,因此您需要使用 Excel 格式来完成您想要的。您可以执行以下操作:

import pandas as pd

# Create a Pandas dataframe from some data.
df = pd.DataFrame([["A1", "B2", "C3"], ["D4", "E5", "F6"]])

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter("test.xlsx", engine='xlsxwriter')

# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = workbook.add_worksheet('Sheet1')

# Add a header format.
header_format = workbook.add_format({'bold': True})

# Write the column headers with the defined format.
for i, row in enumerate(df.values):
for j, val in enumerate(row):
if val=='E5': worksheet.write(i, j, val, header_format)
else: worksheet.write(i, j, val)

# Close the Pandas Excel writer and output the Excel file.
writer.save()

这将输出:

enter image description here

关于python - pandas.to_csv() 一些单词为粗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52819114/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com