2017-04-09 8 views
1

Ich benutze die pandas to_html() Methode, um eine Tabelle für meine Website zu erstellen. Ich möchte einige Attribute zum <table> Tag hinzufügen; aber ich bin mir nicht sicher, wie ich das machen soll.pandas to_html: Attribute zum Tabellen-Tag hinzufügen

my_table = Markup(df.to_html(classes="table")) 

Welche produziert:

<table border="1" class="dataframe table" attribute="value" attribute2="value2"> 

Antwort

1
import re 

df = pd.DataFrame(1, index=[1, 2], columns=list('AB')) 

html = df.to_html(classes="table") 
html = re.sub(
    r'<table([^>]*)>', 
    r'<table\1 attribute="value" attribute2="value2">', 
    html 
) 

print(html.split('\n')[0]) 

<table border="1" class="dataframe table" attribute="value" attribute2="value2"> 
:

<table border="1" class="dataframe table"> 

ich folgendes produzieren wollen

Verwandte Themen