在用 latex 写文章要写结果 table 时,可以写到 excel 中,再用 excel2latex[1] 插件转成 latex table。而在写入 excel 时可以用 pandas.DataFrame
[2] 的 to_excel
函数[3],并:
- 指定行标题(
index
)和列标题(columns
); - 将结果舍入到小数点后 4 位(
float_format
或用 numpy.ndarray 的round
函数); - 不要任何格式(
pd.io.formats.excel.ExcelFormatter.header_style
),参考 [4]。
简例:
- 可能要装个
openpyxl
先:pip install openpyxl
import pandas as pd
import numpy as np
# 不要格式
pd.io.formats.excel.ExcelFormatter.header_style = None
# 可以用 `.round` 舍入
test_results = np.random.rand(4, 3)#.round(4)
df = pd.DataFrame(
test_results,
columns=["metric_1", "metric_2", "metric_3"],
index=["baseline_1", "baseline_2", "baseline_3", "baseline_4"]
)
# 也可以用 `float_format` 舍入
# (tips: 无脑在 to_excel 中指定舍入设置防止忘记 round)
df.to_excel("test.xlsx", float_format="%.4f")
- 效果