首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Jupyter中的pandas的`to_latex`方法来获得可以直接在LaTeX中使用的代码?

如何使用Jupyter中的pandas的`to_latex`方法来获得可以直接在LaTeX中使用的代码?
EN

Stack Overflow用户
提问于 2017-02-12 01:21:12
回答 3查看 2.2K关注 0票数 4

考虑多级索引dataframe

import numpy as np
import pandas as pd
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
   ...:           ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
s = pd.DataFrame(np.random.randn(8, 4), index=arrays)
s

如何使用python代码将其转换为latex中的漂亮表格?

我试过了

print s.to_latex()

但它失败了,并给出了这个结果

File "<ipython-input-45-d2f4611ecc13>", line 1
    print s.to_latex()
          ^
SyntaxError: invalid syntax

我也试过了

s.to_latex()

结果根本就没有组织起来

'\\begin{tabular}{llrrrr}\n\\toprule\n    &     &         0 &         1 &         2 &         3 \\\\\n\\midrule\nbar & one & -0.008518 & -0.535653 & -0.603135 &  0.891277 \\\\\n    & two &  0.191005 & -1.470384 &  0.697245 &  1.054424 \\\\\nbaz & one &  0.414505 & -0.312967 &  0.703490 &  0.933851 \\\\\n    & two & -0.295505 & -0.923843 & -0.423998 &  0.160162 \\\\\nfoo & one & -1.339702 &  0.616059 &  0.519983 &  0.554584 \\\\\n    & two & -1.003784 &  0.674981 &  1.609906 &  0.274486 \\\\\nqux & one & -0.139274 & -0.783004 &  1.084794 &  2.202673 \\\\\n    & two & -0.730785 & -0.468041 &  0.762726 & -0.532599 \\\\\n\\bottomrule\n\\end{tabular}\n'
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42178844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档