Markdown Tip: Displaying Tables


Markdown Cells Tips

To create a table

Create a table using pipes "|" and dashes "-"

Step 1: Change markdown dropdown from "Code" to "Markdown"

In your code cell, type in the following, replacing the text with your own information.

will give you this:

This is
a table

This method is great for simple tables, but not recommended for larger table.

Create a table using IPython.display

Step 1: import dependencies from IPython.display import HTML, display

In [1]:
from IPython.display import HTML, display

Step 2: Create a list of arrays with your text

In [2]:
data = [["Fruits","Dairy","Meats"],
        [3,4,5],
        [6,7,8],
        ]

Step 3: Use the display function. This function is used to display different representations of objects.

In [3]:
from IPython.display import HTML, display

data = [["Fruits","Dairy","Meats"],
        [3,4,5],
        [6,7,8],
        ]

display(HTML(
   '<table><tr>{}</tr></table>'.format(
       '</tr><tr>'.join(
           '<td>{}</td>'.format('</td><td>'.join(str(_) for _ in row)) for row in data)
       )
))
Fruits Dairy Meats
3 4 5
6 7 8

Create a table using IPython.display and tabulate

Step 1: pip install tabulate https://pypi.org/project/tabulate/

In [4]:
from IPython.display import HTML, display
import tabulate

Step 2: Create your table using a list of arrays.

In [5]:
table = [["Fruits","Dairy","Meats"],
         ["Apples","Cheese","Beef"],
         ["Figs","Milk","Chicken"],
         ["Peaches","Yogurt","Rabbit"]
        ]

Step 3: Use the display function to render your table array in html.

In [6]:
display(HTML(tabulate.tabulate(table, tablefmt='html')))
Fruits Dairy Meats
Apples Cheese Beef
Figs Milk Chicken
Peaches Yogurt Rabbit

Create a table using pandas

In [7]:
import pandas as pd

Step 2: Add your table information in an list of lists.

In [8]:
data = [["Apples", "Cheese", "Beef"], ["Figs", "Milk", "Chicken"], ["Peaches","Yogurt","Rabbit"]]

Step 3: Use pandas DataFrame function

In [9]:
pd.DataFrame(data, columns=["Fruit", "Dairy", "Meats"])
Out[9]:
Fruit Dairy Meats
0 Apples Cheese Beef
1 Figs Milk Chicken
2 Peaches Yogurt Rabbit

Create a table using tabletext

Step 1: import tabletext

In [10]:
import tabletext

Step 2: Add your table information in an list of lists.

In [11]:
data = [["Fruit", "Dairy", "Meat"], ["Apples", "Cheese", "Beef"], ["Figs", "Milk", "Chicken"], ["Peaches","Yogurt","Rabbit"]]
In [12]:
print (tabletext.to_text(data))
┌─────────┬────────┬─────────┐
│ Fruit   │ Dairy  │ Meat    │
├─────────┼────────┼─────────┤
│ Apples  │ Cheese │ Beef    │
├─────────┼────────┼─────────┤
│ Figs    │ Milk   │ Chicken │
├─────────┼────────┼─────────┤
│ Peaches │ Yogurt │ Rabbit  │
└─────────┴────────┴─────────┘