to_latex()使用MultiIndex DataFrame生成不正确的结果

时间:2015-06-04 14:37:59

标签: python pandas

我强烈怀疑这是0.16.1的错误,但有人可以确认我不仅仅是个白痴?

df.to_latex()方法打印相对于MultiIndex向下移动一行的数据(但是使用单级索引做正确的事情。)

import pandas as pd
import numpy as np

c = ['GBR', 'USA', 'FRA']
n = 5
x = pd.DataFrame(np.round(np.random.random([n, 3]), 2))
x['from'] = np.random.choice(c, size=n)
x['to'] = np.random.choice(c, size=n)

print x    
print x.set_index('from').to_latex()
print x.set_index(['from', 'to']).to_latex()

这导致:

      0     1     2 from   to
0  0.22  0.45  0.65  GBR  FRA
1  0.14  0.30  0.75  GBR  USA
2  0.10  0.92  0.25  FRA  USA
3  0.78  0.07  0.55  USA  GBR
4  0.24  0.32  0.28  FRA  FRA

# This table looks fine
\begin{tabular}{lrrrl}
\toprule
{} &     0 &     1 &     2 &   to \\
\midrule
from &       &       &       &      \\
GBR  &  0.22 &  0.45 &  0.65 &  FRA \\
GBR  &  0.14 &  0.30 &  0.75 &  USA \\
FRA  &  0.10 &  0.92 &  0.25 &  USA \\
USA  &  0.78 &  0.07 &  0.55 &  GBR \\
FRA  &  0.24 &  0.32 &  0.28 &  FRA \\
\bottomrule
\end{tabular}

# Look! This table is shifted down one row!
\begin{tabular}{llrrr}
\toprule
    &     &     0 &     1 &     2 \\
\midrule
GBR & FRA &       &       &       \\
    & USA &  0.22 &  0.45 &  0.65 \\
FRA &     &  0.14 &  0.30 &  0.75 \\
USA & GBR &  0.10 &  0.92 &  0.25 \\
FRA & FRA &  0.78 &  0.07 &  0.55 \\
\bottomrule
\end{tabular}

0 个答案:

没有答案