Tables in Notebooks¶

Stats 507, Fall 2021

James Henderson, PhD
September 7, 2021

Contents¶

  • Intro
  • Pandas
  • itable

Why?¶

  • Tables are a useful and efficient way to summarize key information.
  • An initial table describing the data used in a notebook or research article provides context for the results to follow.

Table 1¶

  • "Table 1" is a short-hand throughout much of the research world referring to a table of descriptive statistics at the start of a manuscript's result section.
  • "Table 1" is often organized so that columns correspond to levels of a key exposure.
  • See an example [here][1] (refer to Table 2).

[1]: https://journals.plos.org/plosmedicine/article?id=10.1371/journal.pmed.1003730

Tables vs Figures¶

  • Tables are generally more efficient for summarizing a series of discrete summaries.
  • Figures are better for displaying key relationships and distributions.
  • Tables and figures can be complementary to one another.

Modules¶

  • For basic HTML or markdown tables, use pandas.
    • HTML natively
    • For markdown, conda install tabulate
  • For interactive DataTables, use the binding provided by itables.
  • pip install itables

Pandas Methods¶

  • DataFrame objects from pandas have methods:
    • .to_html()
    • .to_markdown().
In [ ]:
%run s ring_df.py #constructs a data frame dat
print(dat.to_html())

Pandas Methods¶

  • DataFrame objects from pandas have methods:
    • .to_html()
    • .to_markdown().
In [ ]:
#%run string_df.py
print(dat.to_markdown())

Displaying HTML¶

  • To render the HTML in the notebook, use display() and HTML() from IPython.
In [ ]:
from IPython.core.display import display, HTML
display(HTML('<h2> Hello <br> World!</h2>'))
display(HTML(dat.to_html(index=False)))

Displaying Markdown¶

  • To render markdown in the notebook, use display() and Markdown() from IPython.
In [ ]:
from IPython.core.display import display, Markdown
display(Markdown('## Hello <br> World!'))
display(Markdown(dat.to_markdown(index=False))) 

Interactive Data Tables¶

  • DataTables is a plug-in for the jQuery Javascript library that facilates interactivity such as search and pagination in HTML tables.
  • Useful for large tables.

itables¶

  • itables provides a Jupyter binding to the DataTables library for pandas DataFrames or series.
  • Use its show() function to display a pandas DataFrame as a datatable in your notebook.
In [ ]:
from itables import show
show(dat)

How it works¶

  • This is out of scope for the course, but, if you're interested, here's a tutorial describing how the binding works.

Takeaways¶

  • Give your notebooks a professional polish by including nicely formatted and thoughtfully structured tables.
  • Unless you are explicitly discussing a dataset, avoid code-oriented variable_namesin favor of well chosen English.