How do I output lists as a table in Jupyter notebook?

Wayne Werner picture Wayne Werner · Feb 2, 2016 · Viewed 114.9k times · Source

I know that I've seen some example somewhere before but for the life of me I cannot find it when googling around.

I have some rows of data:

data = [[1,2,3],
        [4,5,6],
        [7,8,9],
        ]

And I want to output this data in a table, e.g.

+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
| 7 | 8 | 9 |
+---+---+---+

Obviously I could use a library like prettytable or download pandas or something but I'm very disinterested in doing that.

I just want to output my rows as tables in my Jupyter notebook cell. How do I do this?

Answer

ruffsl picture ruffsl · Feb 19, 2017

I just discovered that tabulate has a HTML option and is rather simple to use.
Quite similar to Wayne Werner's answer:

from IPython.display import HTML, display
import tabulate
table = [["Sun",696000,1989100000],
         ["Earth",6371,5973.6],
         ["Moon",1737,73.5],
         ["Mars",3390,641.85]]
display(HTML(tabulate.tabulate(table, tablefmt='html')))

Still looking for something simple to use to create more complex table layouts like with latex syntax and formatting to merge cells and do variable substitution in a notebook:
Allow references to Python variables in Markdown cells #2958