2017-02-11 124 views
0

考虑多级索引数据帧小号如何将多级索引DataFrame转换为乳胶表(Pandas-Jupyter)?

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代码乳胶?

我试图

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' 
举办

回答

0

你期望得到什么?这看起来是正确的,我...

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

u'\\begin{tabular}{llrrrr}\n\\toprule\n &  &   0 &   1 &   2 &   3 \\\\\n\\midrule\nbar & one & -0.792068 & 1.667280 & 0.996496 & 0.278456 \\\\\n & two & -0.558638 & 0.329949 & -0.051455 & 0.586003 \\\\\nbaz & one & 0.400601 & -0.371393 & 1.041784 & -0.956686 \\\\\n & two & -0.007604 & -1.332577 & -1.010781 & -0.448133 \\\\\nfoo & one & 0.567487 & -0.929883 & 0.022723 & 0.945251 \\\\\n & two & 0.009925 & 0.556380 & 0.038697 & 0.067532 \\\\\nqux & one & -1.518476 & -1.706236 & 0.598467 & 1.439247 \\\\\n & two & 0.644754 & 0.929502 & 0.327730 & 0.557668 \\\\\n\\bottomrule\n\\end{tabular}\n' 
+0

非常感谢你这一点,但我期待得到一个正确的标签,你可以在这个标签看到我可能不只是复制它在我的乳胶文件的地方它。例如,它有\ n然后是\ \ toprule,这应该\ n然后\ toprule只有一个\ – rsc05