【问题标题】:How to merge cells in the HTML output of a pandas dataframe in Python如何在 Python 中合并熊猫数据框的 HTML 输出中的单元格
【发布时间】:2020-04-14 10:33:01
【问题描述】:

我有一个看起来像下面这样的 pandas 数据框...

我知道我无法合并数据框本身的单元格,但是有没有一种简单的方法可以在 HTML 输出中合并 Column1 的单元格,以便我得到类似下面的内容?...

【问题讨论】:

标签: python html pandas


【解决方案1】:

这有点笨拙(当我解析 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-25
    • 2014-07-02
    • 2021-05-30
    • 1970-01-01
    • 2021-08-16
    • 2018-11-27
    • 1970-01-01
    相关资源
    最近更新 更多