考虑多级索引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'
https://stackoverflow.com/questions/42178844
复制相似问题