How do I output lists as a table in Jupyter notebook?
I just discovered that tabulate has a HTML option and is rather simple to use.
Update: As of Jupyter v6 and later, the returned table
should just render via the output cell:
import tabulate
data = [["Sun",696000,1989100000],
["Earth",6371,5973.6],
["Moon",1737,73.5],
["Mars",3390,641.85]]
table = tabulate.tabulate(data, tablefmt='html')
table
As for Jupyter v5 or earlier, you may need to be more explicit, similar to Werner's answer:
from IPython.display import HTML, display
display(HTML(table))
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
There is a nice trick: wrap the data with pandas DataFrame.
import pandas as pd
data = [[1, 2], [3, 4]]
pd.DataFrame(data, columns=["Foo", "Bar"])
It displays data like:
| Foo | Bar |
0 | 1 | 2 |
1 | 3 | 4 |