How to inject a table id into pandas.DataFrame.to_html() output?

Question:

Using the follow python code to generate an HTML table from a pandas DataFrame:

IN:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.zeros((2,2)))
df.to_html()
print(df.to_html())

OUT:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0.0</td>
      <td>0.0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>0.0</td>
      <td>0.0</td>
    </tr>
  </tbody>
</table>

Is there an easy way to insert an id into the table start tag?

So that the start tag looks like this:

<table id="my_table" border="1" class="dataframe">
Asked By: James Draper

||

Answers:

The easiest way to do this is to use the Styler interface for generating HTML, which can set arbitrary table properties (set_table_attributes). The resulting HTML is more verbose, because there are a number of extension ids/classes embedded, but should render equivalently.

print(df.style.set_table_attributes('id="my_table"').render())


<style  type="text/css" >
</style>  
<table id="T_6ebfc734_51f7_11e7_b81e_b808cf3e856a" id="my_table"> 
<thead>    <tr> 
        <th class="blank level0" ></th> 
        <th class="col_heading level0 col0" >0</th> 
        <th class="col_heading level0 col1" >1</th> 
    </tr></thead> 
<tbody>    <tr> 
        <th id="T_6ebfc734_51f7_11e7_b81e_b808cf3e856a" class="row_heading level0 row0" >0</th> 
        <td id="T_6ebfc734_51f7_11e7_b81e_b808cf3e856arow0_col0" class="data row0 col0" >0</td> 
        <td id="T_6ebfc734_51f7_11e7_b81e_b808cf3e856arow0_col1" class="data row0 col1" >0</td> 
    </tr>    <tr> 
        <th id="T_6ebfc734_51f7_11e7_b81e_b808cf3e856a" class="row_heading level0 row1" >1</th> 
        <td id="T_6ebfc734_51f7_11e7_b81e_b808cf3e856arow1_col0" class="data row1 col0" >0</td> 
        <td id="T_6ebfc734_51f7_11e7_b81e_b808cf3e856arow1_col1" class="data row1 col1" >0</td> 
    </tr></tbody> 
</table> 
Answered By: chrisb

You can use BeautifulSoup to add an id attribute to the table:

from bs4 import BeautifulSoup
soup = BeautifulSoup(df.to_html(), "html.parser")    
soup.find('table')['id'] = 'my_table'
soup

<table border="1" class="dataframe" id="my_table">
    <thead>
        <tr style="text-align: right;">
            <th></th>
            <th>0</th>
            <th>1</th>
...

To get the html as str, use str(soup).

Answered By: Psidom

I tried this:

df.to_html(classes = 'my_class" id = "my_id')

and I got the following:

<table border="1" class="dataframe my_class" id = "my_id">

I found it here: https://code.i-harness.com/en/q/1d2d2af

Answered By: Jacqueline Garrido

The easiest way is from the snippet I found in the Pandas Documents and uses Pandas

df.to_html( table_id = "my_id")

Output:

<table id = "my_id">

Source: https://pandas.pydata.org/pandas-docs/version/0.23.4/generated/pandas.DataFrame.to_html.html

Answered By: Daniel Burke

This is the correct way to get id / class inserted during dataframe conversion to HTML:

df.to_html(classes = 'class1', table_id = 'id_tab')    

In case we want to include multiple classes, we may assign a list of classes like:

df.to_html(classes = ['class1', 'class2'], table_id = 'id_tab')

Detailed information available here.

Answered By: Love Putin Not War

From the documentation of Styler.set_uuid:

Set the uuid applied to id attributes of HTML elements

For me set_table_attributes created duplicated id attribute in the HTML.

Also as a bonus you get id for all elements inside the table.

IN

import pandas as pd
import numpy as np

df = pd.DataFrame(np.zeros((2,2)))
df.to_html()
print(df.style.set_uuid('table_id').to_html())

OUT

<style type="text/css">
</style>
<table id="T_table_id">
  <thead>
    <tr>
      <th class="blank level0" >&nbsp;</th>
      <th id="T_table_id_level0_col0" class="col_heading level0 col0" >0</th>
      <th id="T_table_id_level0_col1" class="col_heading level0 col1" >1</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th id="T_table_id_level0_row0" class="row_heading level0 row0" >0</th>
      <td id="T_table_id_row0_col0" class="data row0 col0" >0.000000</td>
      <td id="T_table_id_row0_col1" class="data row0 col1" >0.000000</td>
    </tr>
    <tr>
      <th id="T_table_id_level0_row1" class="row_heading level0 row1" >1</th>
      <td id="T_table_id_row1_col0" class="data row1 col0" >0.000000</td>
      <td id="T_table_id_row1_col1" class="data row1 col1" >0.000000</td>
    </tr>
  </tbody>
</table>
Answered By: Hagai Drory
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.