如何用pandas输出乳胶中的多索引DataFrame?

时间:2015-03-20 18:23:05

标签: python pandas latex

我正在尝试使用python和pandas输出乳胶输出中的多索引DataFrame。到目前为止,我有这个:

import pandas as pd

l = []
for a in ['1', '2', '3']:
    for b in ['first', 'second']:
        for c in ['one', 'two']:
            x = 3.2
            s = pd.Series({'a':a, 'b':b, 'c':c, 'x':x})
            l.append(pd.DataFrame(s).transpose())

df = pd.concat(l, ignore_index=True)
df = df.set_index(['a', 'b', 'c'])
print(df)
print(df.to_latex())

但是,我没有预期的输出。

                x
a b      c       
1 first  one  3.2
         two  3.2
  second one  3.2
         two  3.2
2 first  one  3.2
         two  3.2
  second one  3.2
         two  3.2
3 first  one  3.2
         two  3.2
  second one  3.2
         two  3.2
\begin{tabular}{llll}
\toprule

  &       &     &    x \\
\midrule
1 & first & one &      \\
2 & second & two &  3.2 \\
\bottomrule
\end{tabular}

这是一个错误还是我错过了什么?

1 个答案:

答案 0 :(得分:1)

我认为这可能是to_latex中的错误。请参阅this question

根据@ PaulH的评论,您可以执行以下操作:

df.reset_index().to_latex(index=False)

这会输出重复的行标签,所以它比理想的更混乱,但至少它会输出LaTeX中的整个表格。

相关问题