【问题标题】:How to output a multi-index DataFrame in latex with pandas?如何用熊猫在乳胶中输出多索引数据帧?
【发布时间】:2015-06-22 21:42:35
【问题描述】:

我正在尝试使用 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}

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

【问题讨论】:

标签: python pandas latex


【解决方案1】:

我认为这可能是to_latex 中的一个错误。见this question

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

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

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

【讨论】:

    猜你喜欢
    • 2014-11-02
    • 2014-11-13
    • 1970-01-01
    • 2014-01-08
    • 2017-03-05
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    相关资源
    最近更新 更多