簡體   English   中英

How to merge cells in the HTML output of a pandas dataframe in Python

[英]How to merge cells in the HTML output of a pandas dataframe in Python

我有一個 pandas dataframe 看起來像下面這樣......

在此處輸入圖像描述

我知道我無法合並 dataframe 本身的單元格,但有沒有一種簡單的方法可以合並 HTML output 中的 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM