s

Printing pandas dataframe tables

By Angela C

April 22, 2021 in pandas nbconvert Jupyter notebooks

Reading time: 2 minutes.

When printing a DataFrame table in a markdown file that was converted from a Jupyter notebook using nbconvert, the table data may not be displayed as it is raw HTML.

Instead of allowing all raw html to be displayed which may open up a security risk you can instead surround the code with a shortcode to display raw HTML.

  • Use a shortcode contain {{ .Inner }} to display the raw HTML code.
  • Add some styling to the CSS style file to style tables with the “dataframe” class.
  • Alternatively add a bootstrap class to the table tag.

I added some styling for the class dataframe to the style CSS.

See CSS tables

Here is a dataframe table from a Jupyter notebook.

Example 1 table-responsive-sm

package_id description format name url
0 f586470f-d80a-4bec-ae71-0471213a148d API JSON Yesterday's Weather Athenry https://prodapi.metweb.ie/observations/athenry/yesterday
0 dc589d79-fa44-4396-a8cc-db587f695b86 API JSON Yesterday's Weather Ballyhaise https://prodapi.metweb.ie/observations/ballyhaise/yesterday
0 ce4947d6-daec-487e-99d8-2b65baf131b8 API JSON Yesterday's Weather Belmullet https://prodapi.metweb.ie/observations/belmullet/yesterday

Here is another pandas DataFrame table from a Jupyter notebook. This is one where I used the pandas dataframe style option style.hide_index() to not display the index column. It does not have the same table class as the ordinary pandas DataFrames. I am manually adding the table class to the table. This will need to be automated but at least the table output looks nice for now. I guess you would be editing the notebook anyway so it is not such a big deal to add the shortcode. I will learn how to do the shortcode which will include the class and styling of the dataframe tables.s

Example 2 Table surrounded with div class table-responsive-md

package_id description format name url
0 f586470f-d80a-4bec-ae71-0471213a148d API JSON Yesterday's Weather Athenry https://prodapi.metweb.ie/observations/athenry/yesterday
0 dc589d79-fa44-4396-a8cc-db587f695b86 API JSON Yesterday's Weather Ballyhaise https://prodapi.metweb.ie/observations/ballyhaise/yesterday
0 ce4947d6-daec-487e-99d8-2b65baf131b8 API JSON Yesterday's Weather Belmullet https://prodapi.metweb.ie/observations/belmullet/yesterday

Example 3 wrapped in div tag with class table-responsive-md

Here is a pandas DataFrame, I cut the style tags at the top and added the bootstrap class “table-sm table-responsive-lg table-striped” to the opening table tag.

name description url format
91 Dublin (Phoenix Park) Climate Data Daily Climate Data https://cli.fusio.net/cli/climate_data/webdatac/dly1723.csv CSV
93 Dublin (Phoenix Park) Climate Data Monthly Climate Data https://cli.fusio.net/cli/climate_data/webdatac/mly1723.csv CSV
302 PhoenixPark Hourly Data csv file https://cli.fusio.net/cli/climate_data/webdata/hly175.csv CSV
304 Phoenix Park Monthly Data csv file https://cli.fusio.net/cli/climate_data/webdata/mly175.csv CSV
371 Today's weather Phoenix Park CSV Todays Weather https://www.met.ie/latest-reports/observations/download/phoenix-park CSV
427 Yesterday's Weather Phoenix Park CSV Yesterday's Weather https://www.met.ie/latest-reports/observations/download/phoenix-park/yesterday CSV

Wrapped in div tag with class table-responsive-sm

package_id description format name url
0 f586470f-d80a-4bec-ae71-0471213a148d API JSON Yesterday's Weather Athenry https://prodapi.metweb.ie/observations/athenry/yesterday
0 dc589d79-fa44-4396-a8cc-db587f695b86 API JSON Yesterday's Weather Ballyhaise https://prodapi.metweb.ie/observations/ballyhaise/yesterday
0 ce4947d6-daec-487e-99d8-2b65baf131b8 API JSON Yesterday's Weather Belmullet https://prodapi.metweb.ie/observations/belmullet/yesterday

Bootstrap Layout Containers I amended the baseof template to use the Bootstrap container <div class="container-fluid">. .container-fluid is width: 100% at all breakpoints. This will span the entire width of the viewport.