Beginners Guide To Tabulate: Python Tool For Creating Nicely Formatted Tables

In this article, we will see what are the different types of table formatting we can perform using Tabulate.
tabulate

Visualizing the data in tabular form is easier than visualizing it in a paragraph or comma-separated form. Nicely formatted tables not only provide you with a better way of looking at tables it can also help in understanding each data point clearly with its heading and value.

Tabulate is an open-source python package/module which is used to print tabular data in nicely formatted tables. It is easy to use and contains a variety of formatting functions. It has the following functionalities:

  • One function call for all types of formatting
  • Can be downloaded in multiple output formats
  • Provides a better presentation with text and data.

In this article, we will see what are the different types of table formatting we can perform using Tabulate. 

THE BELAMY

Sign up for your weekly dose of what's up in emerging technology.

Implementation:

We will start by installing tabulate using pip install tabulate.

  1. Importing required libraries

We will be using the tabulate function from the tabulate library so we need to import that. Other than this we do not require to import any python module.

from tabulate import tabulate

  1. Creating Formatted Tables

Now we will start by creating different types of formatted tables.

data = [["Himanshu",1123, 10025], ["Rohit",1126,10029], 

             ["Sha",111178,7355.4]]

print(tabulate(data))

Here we can see a plain table which is nicely formatted. Now let’s see how we can add a header to this table that we just created.  

print(tabulate(data, headers=["Name","User ID", "Roll. No."]))

In order to define the header along with the data, we can set that header=’firstrow’, let us see it through an example.

data = [['Name','ID'],["Himanshu",1123], ["Rohit",1126], ["Sha",111178]]

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

We can also display the indices of the rows by using the show index parameter.

data = [['Name','ID'],["Himanshu",1123], ["Rohit",1126], ["Sha",111178]]

print(tabulate(data, headers='firstrow', showindex='always'))

Now let us see what are some of the formats in which we can print the table.

  1. Plain Table

data = [['Name','ID'],["Himanshu",1123], ["Rohit",1126], ["Sha",111178]]

print(tabulate(data, headers='firstrow', showindex='always',    

           tablefmt='plain'))

  1. Fancy Grid

data = [['Name','ID'],["Himanshu",1123], ["Rohit",1126], ["Sha",111178]]

print(tabulate(data, headers='firstrow', showindex='always',  

         tablefmt='fancy_grid'))

Tabulate
  1. Jira

data = [['Name','ID'],["Himanshu",1123], ["Rohit",1126], ["Sha",111178]]

print(tabulate(data, headers='firstrow', showindex='always', tablefmt='jira'))

Tabulate
  1. HTML

data = [['Name','ID'],["Himanshu",1123], ["Rohit",1126], ["Sha",111178]]

print(tabulate(data, headers='firstrow', showindex='always', 

         tablefmt='html'))

Tabulate
  1. Textile

data = [['Name','ID'],["Himanshu",1123], ["Rohit",1126], ["Sha",111178]]

print(tabulate(data, headers='firstrow', showindex='always', 

         tablefmt='textile'))

Tabulate

Similarly, there are many more table formats that we can use to design or format our table.

Conclusion:

In this article, we saw how we can create a table using the data we provide as input. We saw how we can create headers, indices for rows. After that, we saw some of the table formats which are defined in the tabulate library.

More Great AIM Stories

Himanshu Sharma
An aspiring Data Scientist currently Pursuing MBA in Applied Data Science, with an Interest in the financial markets. I have experience in Data Analytics, Data Visualization, Machine Learning, Creating Dashboards and Writing articles related to Data Science.

Our Upcoming Events

Conference, in-person (Bangalore)
Machine Learning Developers Summit (MLDS) 2023
19-20th Jan, 2023

Conference, in-person (Bangalore)
Rising 2023 | Women in Tech Conference
16-17th Mar, 2023

Conference, in-person (Bangalore)
Data Engineering Summit (DES) 2023
27-28th Apr, 2023

Conference, in-person (Bangalore)
MachineCon 2023
23rd Jun, 2023

Conference, in-person (Bangalore)
Cypher 2023
20-22nd Sep, 2023

3 Ways to Join our Community

Whatsapp group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our newsletter

Get the latest updates from AIM