Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas Data Frame save as HTML page

I am trying to save defined in Python Pandas Data Frame as HTML page. In addition i would like to make this table saved as HTML table ability to be filtered by value of any column. Can you please provide possible solution? At the final this should be table saved as HTML page. I would like to incorporate this code in my Python code. Thank you

like image 473
Felix Avatar asked Sep 07 '15 01:09

Felix


People also ask

How do I save a DataFrame as a image in Python?

Pass your normal or styled DataFrame to the export function along with a file location to save it as an image. You may also export directly from the DataFrame or styled DataFrame using the dfi. export and export_png methods, respectively. Here, an example of how exporting a DataFrame would look like in a notebook.


2 Answers

You can use pandas.DataFrame.to_html().

Example:

>>> import numpy as np >>> from pandas import * >>> df = DataFrame({'foo1' : np.random.randn(2),                     'foo2' : np.random.randn(2)}) >>> df.to_html('filename.html') 

This will save the following html to filename.html.

Output:

<table border="1" class="dataframe">    <thead>      <tr style="text-align: right;">        <th></th>        <th>foo1</th>        <th>foo2</th>      </tr>    </thead>    <tbody>      <tr>        <th>0</th>        <td>-0.223430</td>        <td>-0.904465</td>      </tr>      <tr>        <th>1</th>        <td>0.317316</td>        <td>1.321537</td>      </tr>    </tbody>  </table>
like image 159
Sait Avatar answered Sep 22 '22 11:09

Sait


.to_html() also can be used to create html string

import io import pandas as pd from numpy.random import randn  df = pd.DataFrame(     randn(5, 4),     index = 'A B C D E'.split(),     columns = 'W X Y Z'.split() )  str_io = io.StringIO()  df.to_html(buf=str_io, classes='table table-striped')  html_str = str_io.getvalue()  print(html_str) 

<table border="1" class="dataframe table table-striped">    <thead>      <tr style="text-align: right;">        <th></th>        <th>W</th>        <th>X</th>        <th>Y</th>        <th>Z</th>      </tr>    </thead>    <tbody>      <tr>        <th>A</th>        <td>0.302665</td>        <td>1.693723</td>        <td>-1.706086</td>        <td>-1.159119</td>      </tr>      <tr>        <th>B</th>        <td>-0.134841</td>        <td>0.390528</td>        <td>0.166905</td>        <td>0.184502</td>      </tr>      <tr>        <th>C</th>        <td>0.807706</td>        <td>0.072960</td>        <td>0.638787</td>        <td>0.329646</td>      </tr>      <tr>        <th>D</th>        <td>-0.497104</td>        <td>-0.754070</td>        <td>-0.943406</td>        <td>0.484752</td>      </tr>      <tr>        <th>E</th>        <td>-0.116773</td>        <td>1.901755</td>        <td>0.238127</td>        <td>1.996652</td>      </tr>    </tbody>  </table>
like image 24
Valery Ramusik Avatar answered Sep 24 '22 11:09

Valery Ramusik