MITB Banner

Python Tabulate Module – Create Formatted Tables

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

Share

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. 

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

2. 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.

Create Plain Tables

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

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

           tablefmt='plain'))

Create Fancy Grid

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

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

         tablefmt='fancy_grid'))

Create Jira

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

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

HTML

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

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

         tablefmt='html'))

Textile

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

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

         tablefmt='textile'))

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.

Popular Guides

Share
Picture of Himanshu Sharma

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.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Courses & Careers

Become a Certified Generative AI Engineer

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.