How to create an HTML table using a pandas DataFrame ?

Published: March 07, 2023

Updated: March 13, 2023

Tags: Python; Pandas; Dataframe;

DMCA.com Protection Status

To Create an HTML table from a pandas DataFrame, a solution is to use the Pandas library to_html() function which allows the user to convert their DataFrame into an HTML table with just one line of code:

Create a dataframe

To start, import pandas and create your DataFrame using the data you would like to convert into an HTML table:

import pandas as pd
import numpy as np

data = np.arange(1,31)
data = data.reshape(10,3)

df = pd.DataFrame(data, columns=['A','B','C'])

returns

  A   B   C
  0   1   2   3
  1   4   5   6
  2   7   8   9
  3  10  11  12
  4  13  14  15
  5  16  17  18
  6  19  20  21
  7  22  23  24
  8  25  26  27
  9  28  29  30

Convert DataFrame to an HTML table

After your DataFrame is created, simply use the to_html() function and store the output in a variable.

html_code = df.to_html()

Output

'<table border="1" class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      <th>A</th>\n      <th>B</th>\n      <th>C</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4</td>\n      <td>5</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>7</td>\n      <td>8</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>10</td>\n      <td>11</td>\n      <td>12</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>13</td>\n      <td>14</td>\n      <td>15</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>16</td>\n      <td>17</td>\n      <td>18</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>19</td>\n      <td>20</td>\n      <td>21</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>22</td>\n      <td>23</td>\n      <td>24</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>25</td>\n      <td>26</td>\n      <td>27</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>28</td>\n      <td>29</td>\n      <td>30</td>\n    </tr>\n  </tbody>\n</table>'

Note that the output is a string:

type(html_code)

returns

str

Additional features

Remove control characters in string such as \n

html_code = html_code.replace('\n','')

returns

<table border="1" class="dataframe">  <thead>    <tr style="text-align: right;">      <th></th>      <th>A</th>      <th>B</th>      <th>C</th>    </tr>  </thead>  <tbody>    <tr>      <th>0</th>      <td>1</td>      <td>2</td>      <td>3</td>    </tr>    <tr>      <th>1</th>      <td>4</td>      <td>5</td>      <td>6</td>    </tr>    <tr>      <th>2</th>      <td>7</td>      <td>8</td>      <td>9</td>    </tr>    <tr>      <th>3</th>      <td>10</td>      <td>11</td>      <td>12</td>    </tr>    <tr>      <th>4</th>      <td>13</td>      <td>14</td>      <td>15</td>    </tr>    <tr>      <th>5</th>      <td>16</td>      <td>17</td>      <td>18</td>    </tr>    <tr>      <th>6</th>      <td>19</td>      <td>20</td>      <td>21</td>    </tr>    <tr>      <th>7</th>      <td>22</td>      <td>23</td>      <td>24</td>    </tr>    <tr>      <th>8</th>      <td>25</td>      <td>26</td>      <td>27</td>    </tr>    <tr>      <th>9</th>      <td>28</td>      <td>29</td>      <td>30</td>    </tr>  </tbody></table>

Minify html

import htmlmin

html_code = htmlmin.minify(html_code)

print( html_code )

output:

<table border=1 class=dataframe> <thead> <tr style="text-align: right;"> <th></th> <th>A</th> <th>B</th> <th>C</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <th>1</th> <td>4</td> <td>5</td> <td>6</td> </tr> <tr> <th>2</th> <td>7</td> <td>8</td> <td>9</td> </tr> <tr> <th>3</th> <td>10</td> <td>11</td> <td>12</td> </tr> <tr> <th>4</th> <td>13</td> <td>14</td> <td>15</td> </tr> <tr> <th>5</th> <td>16</td> <td>17</td> <td>18</td> </tr> <tr> <th>6</th> <td>19</td> <td>20</td> <td>21</td> </tr> <tr> <th>7</th> <td>22</td> <td>23</td> <td>24</td> </tr> <tr> <th>8</th> <td>25</td> <td>26</td> <td>27</td> </tr> <tr> <th>9</th> <td>28</td> <td>29</td> <td>30</td> </tr> </tbody></table>

Create an html page

<!DOCTYPE html>
<html>
<head>
<style>
.dataframe {
  font-family: Arial, Helvetica, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

.dataframe td, .dataframe th {
  border: 1px solid #ddd;
  padding: 8px;
}

.dataframe tr:nth-child(even){background-color: #f2f2f2;}

.dataframe tr:hover {background-color: #ddd;}

.dataframe th {
  padding-top: 12px;
  padding-bottom: 12px;
  text-align: left;
  background-color: #04AA6D;
  color: white;
}
</style>
</head>
  <body>

<table border=1 class=dataframe> <thead> <tr style="text-align: right;"> <th></th> <th>A</th> <th>B</th> <th>C</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <th>1</th> <td>4</td> <td>5</td> <td>6</td> </tr> <tr> <th>2</th> <td>7</td> <td>8</td> <td>9</td> </tr> <tr> <th>3</th> <td>10</td> <td>11</td> <td>12</td> </tr> <tr> <th>4</th> <td>13</td> <td>14</td> <td>15</td> </tr> <tr> <th>5</th> <td>16</td> <td>17</td> <td>18</td> </tr> <tr> <th>6</th> <td>19</td> <td>20</td> <td>21</td> </tr> <tr> <th>7</th> <td>22</td> <td>23</td> <td>24</td> </tr> <tr> <th>8</th> <td>25</td> <td>26</td> <td>27</td> </tr> <tr> <th>9</th> <td>28</td> <td>29</td> <td>30</td> </tr> </tbody></table>
  </body>
</html>

How to create an HTML table using a pandas DataFrame ?
How to create an HTML table using a pandas DataFrame ?

References

Links Site
to_html() pandas.pydata.org
Image

of