【发布时间】:2020-04-14 10:33:01
【问题描述】:
我有一个看起来像下面这样的 pandas 数据框...
我知道我无法合并数据框本身的单元格,但是有没有一种简单的方法可以在 HTML 输出中合并 Column1 的单元格,以便我得到类似下面的内容?...
【问题讨论】:
-
这能回答你的问题吗? Pandas Data Frame how to merge columns
我有一个看起来像下面这样的 pandas 数据框...
我知道我无法合并数据框本身的单元格,但是有没有一种简单的方法可以在 HTML 输出中合并 Column1 的单元格,以便我得到类似下面的内容?...
【问题讨论】:
这有点笨拙(当我解析 html 文本时),但可能适用于您的特定情况...
df = pd.DataFrame({
'col1': ['A', 'A', 'B', 'B'],
'col2': [1, 2, 3, 4],
'col3': [5, 6, 7, 8]})
df['cleanme'] = 'cleanme'
df = df.set_index(['col1', 'cleanme'])
html = df.to_html(index_names=False)
html = re.sub('<th></th>\n.*<th></th>', '<th>col1</th>', html) # notice your column name here
html = re.sub('<th>cleanme</th>', '', html)
with open('index.html', 'w') as fou:
fou.write(html)
【讨论】: