简体   繁体   English

如何在带有熊猫的乳胶中输出多索引DataFrame?

[英]How to output a multi-index DataFrame in latex with pandas?

I am trying to output a multi-index DataFrame in a latex output using python and pandas. 我正在尝试使用python和pandas在乳胶输出中输出多索引DataFrame。 So far, I have this: 到目前为止,我有这个:

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())

However, I do not have the expected output. 但是,我没有预期的输出。

                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}

Is this a bug or is there something I missed? 这是一个错误还是我错过了一些东西?

I think this may be a bug in to_latex . 我认为这可能是to_latex的错误。 See this question . 看到这个问题

Based on @PaulH's comment, you could do the following: 根据@PaulH的评论,您可以执行以下操作:

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

That will output repeated row labels, so it's more cluttered than would be ideal, but at least it outputs the whole table in LaTeX. 这将输出重复的行标签,因此比理想情况更混乱,但至少它会在LaTeX中输出整个表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM