Easily Create Tables in python

Apart from using the pandas data frames, We can easily create tables using the tabulate library. The first step is to install the tabulate library using pip. For this article, We will be using google colab for demonstration.

After the library, we can use it to create our tables.

Tabular data types supported by tabulate

Using the tabulate function, We can convert any of the following into an easily readable plain-text table.

  • List of lists
  • Lists
  • Dictionaries
  • 2-Dimensional Numpy arrays
  • Numpy record arrays
  • Pandas DataFrame

List of Lists

We have the following code example.

sample_list = [["First Name", "Last Name","Age"], ["John", "Doe", 30],["Mary", "Jane", 28],["Joseph", "Bill", 34]]

Since we are working with a library, We then have to import the library and convert our list of list into a table.

from tabulate import tabulate

The expected output is what we obtain below.

Since in our list, the first list contains the column names, we can set it as the header of the table by using “firstrow” as the argument for the header element.

print(tabulate(sample_list, headers='firstrow'))

We can also add grids to our data table by using the tablefmt parameter, which gives us the ability to improve our tables’ appearance. If we use “fancy_grid”, we get a table with full lines.

print(tabulate(sample_list, headers='firstrow', tablefmt='fancy_grid'))


We can duplicate the above results using a dictionary.

dict = {'First Name':['John','Mary','Joseph'], 'Last Name':['Doe', 'Jane', 'Bill'], 'Age':['30','28','34']}

For Dictionaries, the keys will be used for the table header and the values used as the table values. To do this, we specify the keyword ‘keys’ as the argument for the headers parameter.

print(tabulate(dict, headers='keys'))

Also, we can use the tablefmt parameter to improve the table’s appearance

The final thing we can do is add the index column. We do this by adding the “showindex” parameter and setting it to True.

Code used: https://colab.research.google.com/drive/19zcAk2SyWI58zTBPAm4bi63I3KGiU2e1?usp=sharing

That brings us to the end of this tutorial on tabulate tables. Thank you for reading and I hope the article was helpful

2 thoughts on “Easily Create Tables in python”

Leave a Comment

Scroll to Top
Need Help!