Comment créer un tableau HTML à partir d'un DataFrame pandas ?

Published: 21 septembre 2023

Tags: Python; Pandas; Dataframe;

DMCA.com Protection Status

Pour créer un tableau HTML à partir d'un DataFrame pandas, une solution consiste à utiliser la fonction to_html() de la bibliothèque Pandas, qui permet à l'utilisateur de convertir leur DataFrame en un tableau HTML en une seule ligne de code :

Créez un dataframe.

Pour commencer, importez pandas et créez votre DataFrame en utilisant les données que vous souhaitez convertir en un tableau HTML :

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'])

donne

  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

Convertir DataFrame en table HTML

Après avoir créé votre DataFrame, utilisez simplement la fonction to_html() et stockez la sortie dans une variable.

html_code = df.to_html()

donne

'<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>'

Notez que la sortie est une chaîne de caractères :

type(html_code)

donne

str

Fonctionnalités supplémentaires

Supprimer les caractères de contrôle dans la chaîne tels que \n

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

donne

<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 )

donne

<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>

Créer une page HTML

<!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>

Comment créer un tableau HTML à partir d'un DataFrame pandas ?
Comment créer un tableau HTML à partir d'un DataFrame pandas ?

Références

Liens Site
to_html() pandas.pydata.org
Image

of