Pandas Styler.to_latex() - 如何传递命令并进行简单编辑

Ale*_*erg 0 python latex pandas pandas-styles

如何将以下命令传递到 Latex 环境中?

\centering(我需要横向表格居中)

\caption*(我需要跳过面板的表格编号)

此外,我需要在 t 统计量中添加括号星号,这意味着数据帧上的特定于行的格式。

例如:

当前的

多变的 价值
常量 2.439628
统计时间 13.921319
家族企业 0.114914
统计时间 0.351283
创始人 0.154914
统计时间 2.351283
调整后的 R 方 0.291328

我要这个

多变的 价值
常量 2.439628
统计时间 (13.921319)***
家族企业 0.114914
统计时间 (0.351283)
创始人 0.154914
统计时间 (1.651283)**
调整后的 R 方 0.291328

我正在 DataSpell 上写研究论文。所有实证工作都在 Python 中进行,然后我使用 Latex (TexiFy) 在 DataSpell 中创建 pdf。由于这个工作流程,我无法在乳胶代码中编辑表格,因为每次运行 jupyter 笔记本时它们都会被覆盖。

如果有帮助的话,下面是我如何将表传递到乳胶环境的示例:

# drop index to column
panel_a.reset_index(inplace=True)


# write Latex index and cut names to appropriate length

ind_list = [
    "ageFirm",
    "meanAgeF",
    "lnAssets",
    "bsVol",
    "roa",
    "fndrCeo",
    "lnQ",
    "sic",
    "hightech",
    "nonFndrFam"
]


# assign the list of values to the column
panel_a["index"] = ind_list

# format column names
header = ["", "count","mean", "std", "min", "25%", "50%", "75%", "max"]

panel_a.columns = header

with open(
    os.path.join(r"/.../tables/panel_a.tex"),"w"
) as tf:
    tf.write(
        panel_a
        .style
        .format(precision=3)
        .format_index(escape="latex", axis=1)
        .hide(level=0, axis=0)
        .to_latex(
            caption = "Panel A: Summary Statistics for the Full Sample",
            label = "tab:table_label",
            hrules=True,

    ))
Run Code Online (Sandbox Code Playgroud)

our*_*os1 5

你一并问了三个问题。我想我可以为你做三分之二(我听说“还不错”)。

  1. 如何使用传递\centeringLaTeX环境Styler.to_latex

使用position_float参数。简化:

df.style.to_latex(position_float='centering')
Run Code Online (Sandbox Code Playgroud)
  1. 如何通过\caption*

这个我不知道。也许有用:为什么标题不起作用

  1. 如何应用特定于行的格式?

这个有点棘手。让我举一个例子来说明我通常会如何做到这一点:

df = pd.DataFrame({'a':['some_var','t stat'],'b':[1.01235,2.01235]})
df.style.format({'a': str, 'b': lambda x: "{:.3f}".format(x) 
                if x < 2 else '({:.3f})***'.format(x)})
Run Code Online (Sandbox Code Playgroud)

结果:

您可以从这个示例中看到它style.format接受 a callable(这里嵌套在 a 内dict,但您也可以这样做:).format(func, subset='value')因此,如果每个值本身都被评估( ) ,那就太好了x < 2

您的情况的问题是评估超过了其他一些值,即(未提供的)P 值与 的结合panel_a['variable'] == 't stat'。现在,假设您在不同的列中有这些 P 值,我建议您创建一个for loop来填充 a list,如下所示:

fmt_list = ['{:.3f}','({:.3f})***','{:.3f}','({:.3f})','{:.3f}','({:.3f})***','{:.3f}']
Run Code Online (Sandbox Code Playgroud)

现在,我们可以将函数应用于df.style.format,并从列表中弹出/选择,如下所示:

fmt_list = ['{:.3f}','({:.3f})***','{:.3f}','({:.3f})','{:.3f}','({:.3f})***','{:.3f}']

def func(v):
    fmt = fmt_list.pop(0)
    return fmt.format(v)

panel_a.style.format({'variable': str, 'value': func})
Run Code Online (Sandbox Code Playgroud)

结果:

无可否认,这个解决方案有点“hacky”,因为修改list函数内部的全局声明远不是一个好的实践;例如,如果您在调用之前再次修改列表func,则其功能不太可能导致预期的行为或更糟,它可能会引发难以追踪的错误。除了简单地将所有浮点数转换为panel_a.value inplace中的字符串之外,我不知道如何解决这个问题。在这种情况下,当然,你不再需要了.format,但它会改变你的df,这也不理想。我想你可以先复制一份(df2 = df.copy()),但这会影响记忆。

无论如何,希望这会有所帮助。因此,您可以将其完整添加到您的代码中,如下所示:

fmt_list = ['{:.3f}','({:.3f})***','{:.3f}','({:.3f})','{:.3f}','({:.3f})***','{:.3f}']

def func(v):
    fmt = fmt_list.pop(0)
    return fmt.format(v)

with open(fname, "w") as tf:
    tf.write(
        panel_a
        .style
        .format({'variable': str, 'value': func})
        ...
        .to_latex(
            ...
            position_float='centering'
    ))
Run Code Online (Sandbox Code Playgroud)