如何在导出到 Excel 文件之前从 Python DataFrame 设置多行样式
Posted
技术标签:
【中文标题】如何在导出到 Excel 文件之前从 Python DataFrame 设置多行样式【英文标题】:How to style several rows from Python DataFrame before exporting to Excel file 【发布时间】:2021-06-10 16:32:22 【问题描述】:我希望我的 DataFrame 最后 3 行打印为粗体。下面是我的 DataFrame 结构,包括文本和图像(从 Jupyter Notebook 输出)格式。
# Actually, there are some more columns but it's not necessary to put them all here.
# These columns are enough to give an example.
epoch loss accuracy f1_m precision_m recall_m lr
1 1.0 5.104000 0.494600 0.458400 0.588500 0.403900 0.001
2 2.0 3.313200 0.801000 0.799500 0.828500 0.772600 0.001
3 3.0 3.063600 0.825000 0.824400 0.852900 0.797700 0.001
4 4.0 2.855900 0.838900 0.838600 0.865700 0.813100 0.001
5 5.0 2.659400 0.847500 0.846100 0.876500 0.817800 0.001
6 6.0 2.490200 0.851100 0.851600 0.876800 0.827900 0.001
7 7.0 2.334100 0.854100 0.853400 0.880100 0.828200 0.001
8 8.0 2.168300 0.862000 0.861800 0.885700 0.839100 0.001
9 9.0 2.025500 0.864200 0.863800 0.888800 0.840300 0.001
10 10.0 1.900300 0.864100 0.863900 0.889100 0.840200 0.001
11 11.0 1.774600 0.866300 0.866200 0.888800 0.844700 0.001
12 12.0 1.668100 0.867000 0.866000 0.891700 0.841700 0.001
13 13.0 1.557800 0.869900 0.869600 0.893500 0.846900 0.001
14 14.0 1.459500 0.871900 0.872000 0.895900 0.849400 0.001
15 15.0 1.386500 0.869000 0.868600 0.892800 0.845700 0.001
16 16.0 1.294400 0.872900 0.873000 0.895800 0.851400 0.001
17 17.0 1.210600 0.876000 0.875700 0.898500 0.853900 0.001
18 18.0 1.156100 0.875800 0.875600 0.897800 0.854500 0.001
19 19.0 1.100800 0.872800 0.872100 0.897200 0.848300 0.001
20 20.0 1.042000 0.875000 0.875200 0.897100 0.854400 0.001
21 21.0 0.991800 0.875700 0.876400 0.899100 0.854900 0.001
22 22.0 0.942500 0.878600 0.878300 0.900800 0.856900 0.001
23 23.0 0.896600 0.878800 0.878400 0.901900 0.856200 0.001
24 24.0 0.857100 0.880500 0.880200 0.901800 0.859700 0.001
25 25.0 0.826900 0.878600 0.878400 0.900600 0.857300 0.001
26 26.0 0.795000 0.878600 0.878100 0.901900 0.855600 0.001
27 27.0 0.778700 0.876300 0.875800 0.900400 0.852500 0.001
28 28.0 0.750800 0.876100 0.875700 0.899300 0.853300 0.001
29 29.0 0.718600 0.878900 0.878900 0.901900 0.857000 0.001
30 30.0 0.707000 0.876000 0.875400 0.899100 0.852900 0.001
31 31.0 0.694900 0.875800 0.875900 0.900000 0.853100 0.001
32 32.0 0.665100 0.879300 0.879100 0.900600 0.858600 0.001
33 33.0 0.657700 0.875600 0.875400 0.899700 0.852500 0.001
34 34.0 0.637700 0.878100 0.878100 0.901000 0.856300 0.001
35 35.0 0.618600 0.880100 0.879500 0.902900 0.857300 0.001
36 36.0 0.601200 0.880400 0.880600 0.902700 0.859600 0.001
37 37.0 0.589900 0.882300 0.882100 0.903400 0.861800 0.001
38 38.0 0.580500 0.880400 0.880600 0.901800 0.860400 0.001
39 39.0 0.573000 0.880200 0.880300 0.902100 0.859600 0.001
40 40.0 0.555900 0.882200 0.881700 0.904700 0.859900 0.001
41 41.0 0.551200 0.880700 0.880300 0.902900 0.858800 0.001
42 42.0 0.547500 0.880400 0.879900 0.903300 0.857600 0.001
43 43.0 0.531100 0.883500 0.883500 0.905500 0.862600 0.001
44 44.0 0.537000 0.880000 0.879500 0.903300 0.856900 0.001
45 45.0 0.526200 0.883200 0.882200 0.904300 0.861200 0.001
46 46.0 0.517900 0.882000 0.882300 0.903000 0.862500 0.001
47 47.0 0.514400 0.881100 0.880900 0.902100 0.860700 0.001
48 48.0 0.505000 0.883400 0.882700 0.904800 0.861600 0.001
49 49.0 0.499200 0.883300 0.883600 0.904600 0.863500 0.001
50 50.0 0.491200 0.884600 0.884700 0.905600 0.864800 0.001
min 1.0 0.491200 0.494600 0.458400 0.588500 0.403900 0.001
max 50.0 5.104000 0.884600 0.884700 0.905600 0.864800 0.001
mean 25.5 1.285006 0.857558 0.855906 0.883479 0.832423 0.001
对于初学者,我尝试使用来自this 邮政编码的 2 个答案来测试它是否适用于最后一行,但它没有用。
还有其他想法来设置 DataFrame 的样式吗?
以防万一,我在这里附上我当前的完整代码:
import fileinput
import glob
import pathlib
import pandas as pd
import os
import numpy as np
def readText(filename):
with open(filename, "r") as f:
data = f.readlines()
return data
# I want my DataFrame to be exported to Excel files later on
def toExcelFiles(filename, data_text):
data = []
for i, line in enumerate(data_text.split('\n')[1::2], start=1):
obj =
obj['epoch'] = i
for x in line.split(' - ')[2:]:
k, v = x.split(':')
obj[k] = v.lstrip()
data.append(obj)
df = pd.DataFrame(data).astype(float)
filename = filename.replace(".txt", ".xlsx")
print(filename)
df.index = np.arange(1, len(df) + 1)
df.loc['min'] = df.min()
df.loc['max'] = df.max()
df.loc['mean'] = df.mean()
df.style.applymap('font-weight: bold', subset=pd.IndexSlice[df.index[df.index=='mean'], :])
with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also
print(df)
df.to_excel(filename)
# main
os.chdir(f'C:/Users/Dionisius/OneDrive/testresult/arsi 4/txt')
file_list = glob.glob("*.txt")
print("File_list: ", file_list)
print("*"*127)
for file in file_list:
text = readText(file) # returns list
string_epoch = ''.join(text) # mengubah list jadi string
toExcelFiles(file, string_epoch)
.txt
的示例文件附在here 上。
【问题讨论】:
【参考方案1】:给你:
def bold_last(df, numrows=3):
ret = pd.DataFrame('', index=df.index, columns=df.columns)
# last few rows
ret.iloc[-numrows:] = 'font-weight:bold'
return ret
# pass numrows=4 to `apply` to bold 4 last rows
df.style.apply(bold_last, axis=None).to_excel('some_file.xlsx')
最后几行看起来像这样:
【讨论】:
.iloc[-:]
代码正是我正在寻找的。现在我还可以使用background-color:yellow
设置最后一行的样式。谢谢!以上是关于如何在导出到 Excel 文件之前从 Python DataFrame 设置多行样式的主要内容,如果未能解决你的问题,请参考以下文章
如何将MYSQL中数据导出到EXCEL表中 python 脚本?