Create Tables using Python

Python is a very easy and versatile programming language. It offers libraries and modules for almost every task that you can think about. While working with data using python sometimes it becomes difficult to present it in a tabular format by using the standard formatting functions provided by Python. So in this article, I will take you through a tutorial on the tabulate module to create tables using Python.

Tabulate Module in Python 

The tabulate module in Python allows us to create and display data in a tabular format which makes the data look more readable. It can be used to organize your data to make it more understandable. Below are some of the data structures in Python which are supported by the tabulate module:

  1. Lists
  2. Dictionary
  3. NumPy Array
  4. Pandas DataFrame

Also, Read – Python Projects with Source Code: Solved and Explained.

The tabulate module doesn’t come preinstalled in the Python standard library so you can easily install it by using the pip command; pip install tabulate.

Create Tables using Python

I hope you now have understood some of the important features provided by the tabulate module in Python. Now let’s see how to create tables using Python by using the tabulate module. Below is how you can create a very simple table using Python:

from tabulate import tabulate
data = [["Name", "Place", "Gender"], ["Aman", "New Delhi", "Male"], ["Hritika", "New Delhi", "Female"], ["Krishna", "UP", "Male"]]
print(tabulate(data))
-------  ---------  ------
Name     Place      Gender
Aman     New Delhi  Male
Hritika  New Delhi  Female
Krishna  UP         Male
-------  ---------  ------

The code above formats lists into a tabular format, now let’s have a look at how we can separate the headers from the values:

print(tabulate(data, headers='firstrow'))
Name     Place      Gender
-------  ---------  --------
Aman     New Delhi  Male
Hritika  New Delhi  Female
Krishna  UP         Male

We can also design this table by adding a grid, here is how you can do it:

print(tabulate(data, headers='firstrow', tablefmt='grid'))
+---------+-----------+----------+
| Name    | Place     | Gender   |
+=========+===========+==========+
| Aman    | New Delhi | Male     |
+---------+-----------+----------+
| Hritika | New Delhi | Female   |
+---------+-----------+----------+
| Krishna | UP        | Male     |
+---------+-----------+----------+

We can also make the grid look better:

print(tabulate(data, headers='firstrow', tablefmt='fancy_grid'))
╒═════════╤═══════════╤══════════╕
│ Name    │ Place     │ Gender   │
╞═════════╪═══════════╪══════════╡
│ Aman    │ New Delhi │ Male     │
├─────────┼───────────┼──────────┤
│ Hritika │ New Delhi │ Female   │
├─────────┼───────────┼──────────┤
│ Krishna │ UP        │ Male     │
╘═════════╧═══════════╧══════════╛

So this is how you can present your data in the form of tables. It is a good approach to format the data into tables as it makes the data look more readable. I hope you liked this article on how to create tables using Python. Feel free to ask your valuable questions in the comments section below.

Default image
Aman Kharwal
Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder
Articles: 1054

Leave a Reply