熊猫系列到多列html

时间:2013-05-24 16:23:21

标签: pandas

我有一个Pandas系列(对应于单个记录的较大DF的片段),我想显示为html。虽然我可以将Series转换为DataFrame并使用.to_html()函数,但这将导致两列html输出。为了节省空间/提供更好的宽高比,我想返回一个四列或六列HTML表,其中系列已被包装成两组或三组索引值列,如此。

import pandas as pd
s = pd.Series( [12,34,56,78,54,77], index=['a', 'b', 'c', 'd', 'e','f'])
#pd.DataFrame(s).to_html()
#will yield 2 column hmtl-ed output 
# I would like a format like this:
a 12  d 78
b 34  e 54
c 56  f 77

这就是我目前的情况:

x = s.reshape((3,2))
y = s.index.reshape((3,2))
new = [ ]
for i in range(len(x)):
    new.append(y[i])
    new.append(x[i])
 z = pd.DataFrame(new)
 z.T.to_html(header=False, index=False)

有没有更好的方式让我错过?

感谢 zach cp

1 个答案:

答案 0 :(得分:1)

这更简单。排序略有不同(跨行读取,而不是向下列),但我不确定这对您是否重要。

In [17]: DataFrame(np.array([s.index.values, s.values]).T.reshape(3, 4))
Out[17]: 
   0   1  2   3
0  a  12  b  34
1  c  56  d  78
2  e  54  f  77

在您的示例中,您需要省略HTML中的“标题”和索引。

相关问题