0

I have a Pandas series (slice of larger DF corresponding to a single record) that I would like to display as html. While i can convert the Series to a DataFrame and use the .to_html() function this will result in a two-column html output. To save space/give a better aspect ratio I would like to return a four- or six-column HTML table where the series has been wrapped into two or three sets of index-value columns, like so.

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

This is what I have currently:

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)

Is there a better way that I might have missed?

thanks zach cp

4

1 回答 1

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 中省略“标题”和索引。

于 2013-05-24T17:06:00.393 回答