python - Pandas 数据帧 to_latex : how to fit tables on a page?

标签 python python-2.7 latex pandas

我正在尝试使用 pandas to_latex() 函数来创建包含我的表格的 PDF,但每次尝试时我都会得到一个

Overfull \hbox (914.42409pt too wide) in paragraph at lines 1--43

或类似的错误。我知道这是因为我的表格对于页面来说太大了,我的问题是,如何让我的表格变小? to_latex 函数的文档似乎不是很广泛,所以我对解决方案的搜索并不是很有成果。

我创建表格和 .tex 文件的代码是这样的:

r = r.to_latex(na_rep = '0', float_format = fp(2),  formatters=format_dict)
message = "\documentclass[12pt]{article}" \
      "\\"+"begin{document} " \
      "\section{20 Day Trading Stats}"
footer = "\end{document}"
message = message + r + footer
afile=open("outputfile.tex", "wb")
afile.write(message)

其中 fp(2)format_dict 只是参数的格式规范。

如果重要的话,我正在使用 MikTeX 和 LEd 构建 PDF,并且我的 PC 运行的是 Windows 7。

最佳答案

一个解决方案可能是 matrix2latex .此模块允许您将表格置于您喜欢的任何类型的环境中。通常,我会选择:

from matrix2latex import matrix2latex
import numpy
data = numpy.array(r)
matrix2latex(data, 'outputfile.tex', 'table', 'tabular', 'small')

这将导致:

\begin{table}[ht]
\begin{tabular}{cc...cc}
\toprule
\begin{small}
item1 & item2 &..... \\
...
\end{small}
\end{tabular}
\end{table}

显然,您可以将“小”更改为您喜欢的任何内容,但请记住参数的顺序很重要。

希望对您有所帮助!

关于python - Pandas 数据帧 to_latex : how to fit tables on a page?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17694593/

相关文章:

python - 我如何转换不同的日期 python 格式?

python - 在 python 中使用 2D 列表的优雅方式

python-2.7 - 根据索引合并 Pandas 行(时间序列)

templates - 如何使用 Emacs+AUCTeX 创建(自动插入)LaTeX 模板?

python - 如何从数据帧在 keras flow 中提供一个热编码矢量数据帧

python - 对如何在 Django-social-auth 中应用 google 和 yahoo OpenID 感到困惑

python - 无法在 Google Datalab Jupyter 笔记本中安装 pyproj 模块

mysql - PHPMyAdmin LaTeX 导出不显示行注释

latex - 根据使用的字体有条件地执行宏

python - 在 Bokeh LabelSet 中更改文本字体大小