from functools import partial
import pandas as pd
import numpy as np
def bold_formatter(x, value, num_decimals=2):
"""Format a number in bold when (almost) identical to a given value.
Args:
x: Input number.
value: Value to compare x with.
num_decimals: Number of decimals to use for output format.
Returns:
String converted output.
"""
# Consider values equal, when rounded results are equal
# otherwise, it may look surprising in the table where they seem identical
if round(x, num_decimals) == round(value, num_decimals):
return f"{{\\bfseries\\num{{{x:.{num_decimals}f}}}}}"
else:
return f"\\num{{{x:.{num_decimals}f}}}"
df = pd.DataFrame(np.array([[1.123456, 2.123456, 3.123456, 4.123456],
[11.123456, 22.123456, 33.123456, 44.123456],
[111.123456, 222.123456, 333.123456, 444.123456],]),
columns=['a', 'b', 'c', 'd'])
col_names = ['a in \\si{\\meter}',
'b in \\si{\\volt}',
'c in \\si{\\seconds}',
'd']
# Colums to format with maximum condition and 2 floating decimals
max_columns_2f = ['a']
# Colums to format with minimum condition and 2 floating decimals
min_columns_2f = ['b', 'c']
# Colums to format with minimum condition and 4 floating decimals
min_columns_4f= ['d']
fmts_max_2f = {column: partial(bold_formatter, value=df[column].max(), num_decimals=2) for column in max_columns_2f}
fmts_min_2f = {column: partial(bold_formatter, value=df[column].min(), num_decimals=2) for column in min_columns_2f}
fmts_min_4f = {column: partial(bold_formatter, value=df[column].min(), num_decimals=4) for column in min_columns_4f}
fmts = dict(**fmts_max_2f, **fmts_min_2f, **fmts_min_4f)
with open("test_table.tex", "w") as fh:
df.to_latex(buf=fh,
index=False,
header=col_names,
formatters=fmts,
escape=False)
import numpy as np
import pandas as pd
# generate a dataframe with 10 rows and 4 columns filled with random numbers
df = pd.DataFrame(data=np.random.rand(10, 4), index= [f"row_{i}" for i in range(10)], columns=[f"col_{i}" for i in range(4)])
# apply some formatting for all numbers (optional)
df_s = df.style.format("{:.2f}")
# loop through rows and find which column for each row has the highest value
for row in df.index:
col = df.loc[row].idxmax()
# redo formatting for a specific cell
df_s = df_s.format(lambda x: "\\textbf{" + f'{x:.2f}' + "}", subset=(row, col))
print(df_s.to_latex())
2条答案
按热度按时间fnatzsnv1#
已经有一个PR Request,将来会支持这个,根据这条评论,很可能会与pandas 1.3.0一起发布。同时,我在另一个issue中发现了这个解决方案,它只强调每列的最大值:
ajsxfq5m2#
循环遍历行以查找最大值的位置,并在这些特定位置应用格式设置。
结果: