nbconvert multiindex dataframes to latex [英] nbconvert multiindex dataframes to latex

查看:120
本文介绍了nbconvert multiindex dataframes to latex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ipython的nbconvert
将多索引Pandas DataFrame导出到latex,但是多索引行出错了。
我在代码开头使用以下代码正确转换为乳胶(我发现它在SO上但不记得在哪里):

I'm trying to export a multi-index Pandas DataFrame to latex using ipython's nbconvert but the multi-index rows come out all wrong. I'm using the following code at the beginning of the code to convert to latex properly (I found it somewhere on SO but can't remember where):

from sympy import latex
from IPython.display import HTML, Latex, display, Math
pd.set_option('display.notebook_repr_html', True)
def _repr_latex_(self):
    return "\\begin{center} %s \end{center}" % self.to_latex()
pd.DataFrame._repr_latex_ = _repr_latex_  # monkey patch pandas DataFrame

groupby代码相当大,但我也用较小的代码进行了测试,如:

the groupby code is pretty large but I have tested it also with smaller codes like:

a = np.array([[1, 3, 4, 5],
             [1, 5, 36, 2],
             [3, 6, 23, 5],
             [2, 2, 1, 6],
             [2, 5, 1, 99]])
df = pd.DataFrame(a, columns=['A','B','C','D'])
df.groupby(by=['A','D']).sum()

结果是

    \begin{center} \begin{tabular}{lrr}
\toprule
{} &  B &   C \\
A D  &    &     \\
\midrule
1 2  &  5 &  36 \\
  5  &  3 &   4 \\
2 6  &  2 &   1 \\
  99 &  5 &   1 \\
3 5  &  6 &  23 \\
\bottomrule
\end{tabular}
 \end{center}

此示例仅显示第一个问题,此输出将显示多个索引堆叠在另一个之上,但我无法在输出之前找到格式化它的方法。 (我正在制作这种类型的许多大型桌子,因此在乳胶本身上形成会[而且]是一种痛苦)。还有一些多索引,它完全不可读。第二个大问题是Ipython使用display()渲染这个表非常好地将列宽调整为屏幕,但是在latex上它会超出页面宽度并且大部分表都丢失了。

This example shows only the first of the problems, this output will show the multiindex stacked one on top of the other, but I can't find a way of formatting it before output. (I am producing many large tables of this sort so formating on latex itself would [and is] a pain). also with a couple of multi-index more, it gets totally unreadable. The second big problem is that Ipython renders this tables with display() really nicely adjusting column width to screen, but on latex it exceeds the page width and most of the table is lost.

我已经搜遍了nbconvert的更好的格式化解决方案,但找不到任何东西。如果你也有这个问题,或者你知道解决这两个问题的任何一个,请告诉我。

I have searched all over for a better formating solution for nbconvert but couldn't find a thing. Please if you have had this problem also or you know a solution to any of this two problems please tell me.

pd:我正在使用python 2.7.7 Anaconda 2.0 .1(64位)和最新版本的pandas(0.14.1)和ipython(2.2.0)。

pd: I'm using python 2.7.7 Anaconda 2.0.1 (64-bit) and the latest versions of pandas(0.14.1) and ipython(2.2.0).

推荐答案

我认为这是 to_latex 中的错误,结果 res.T.to_latex()看起来也不正确。

I think this is a bug in to_latex, and the result of res.T.to_latex() doesn't look right either.

解决方法可能是修改index:

A workaround might be to modify the index:

In [11]: res = df.groupby(by=['A','D']).sum()

In [12]: res.index = res.index.map(lambda x: ' & '.join(map(str, x)))

In [13]: res.index.name = 'A & D'

In [14]: res.columns.values[0] = ' & ' + res.columns[0]

In [15]: print res.to_latex(escape=False)  # the whole point is not to escape the &s
\begin{tabular}{lrr}
\toprule
{} &   & B &   C \\
\midrule
A & D  &       &     \\
1 & 2  &     5 &  36 \\
1 & 5  &     3 &   4 \\
2 & 6  &     2 &   1 \\
2 & 99 &     5 &   1 \\
3 & 5  &     6 &  23 \\
\bottomrule
\end{tabular}

这篇关于nbconvert multiindex dataframes to latex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆