gpt4 book ai didi

python - 如何从 pandas.to_latex 的输出中删除多余的反斜杠?

转载 作者:行者123 更新时间:2023-12-01 01:04:32 27 4
gpt4 key购买 nike

我正在使用 pandas to_latex 函数将 pandas 数据框导出为可以粘贴到 LaTeX 文件中的文本。

这是一个数据框示例:

import pandas as pd

d = {'Country': ['Burma', 'Burundi'], 'Capital': ['Naypyidaw','Gitega']}
df = pd.DataFrame(data=d)

我将 to_latexindex=False 一起使用,以便不导出行名称:

df.to_latex(index=False)

这会产生以下文本 block :

\\begin{tabular}{ll}\n\\toprule\n   
Capital & Country \\\\\n\\midrule\n
Naypyidaw & Burma \\\\\n
Gitega & Burundi \\\\\n
\\bottomrule\n
\\end{tabular}\n

是否有一个参数可以调整以消除这些反斜杠和额外的 \n

这是我想要的输出:

\begin{tabular}{ll}
Capital & Country \\
Naypyidaw & Burma \\
Gitega & Burundi
\end{tabular}

任何帮助将不胜感激!

最佳答案

df.to_latex(index=False)返回一个字符串。如果您想要它的“处理”值,请使用print函数

print(df.to_latex(index=False))

然后使用输出。

关于python - 如何从 pandas.to_latex 的输出中删除多余的反斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55502899/

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