How to easily create tables in Python using tabulate function
Hello friends! In this tutorial, we are going to learn how to create well-formatted tables in Python using tabulate function and how to use different arguments and parameters to change the appearance of your table.
It’s a win-win situation for users to quickly organize their data into a more readable format to better understand the data. Tabulate function in Python offers this opportunity to easily transform regular tabular data into well-formatted text tables. This function can be very useful for analyzing the data in Python.
We will go through the step-by-step process to understand it.
Step 1: Install tabulate library
So first of all, we will install tabulate library with the help of pip install in the command line of Python.
pip install tabulate
So after this tabulate installation will be done in your Python.
Step 2: Import tabulate function in your code.
Now we are going to import it into our code by using the line given below.
from tabulate import tabulate
So now we are ready to use tabulate function in our code.
Step 3:
For example, if we have the following list of lists:
table = [['First Name','Last Name','Age'],['Aayushi','Agrawal',20],['Vivek','Agrawal',50],['Archana','Agrawal',48]] print(tabulate(table))
---------- --------- --- First Name Last Name Age Aayushi Agrawal 20 Vivek Agrawal 50 Archana Agrawal 48 ---------- --------- ---
print(tabulate(table,headers='firstrow'))
As you can see, in the above output the first list contains the names of columns as their elements but we can change it into column name or header names by passing the ‘firstrow’ argument.
Output:
First Name Last Name Age ----------- ----------- ----- Aayushi Agrawal 20 Vivek Agrawal 50 Archana Agrawal 48
Now we are going to use tablefmt parameter, which allows us to improve the appearance of our table.
print(tabulate(table,headers='firstrow',tablefmt='grid'))
Output:
+--------------+-------------+-------+ | First Name | Last Name | Age | +==============+=============+=======+ | Aayushi | Agrawal | 20 | +--------------+-------------+-------+ | Vivek | Agrawal | 50 | +--------------+-------------+-------+ | Archana | Agrawal | 48 | +--------------+-------------+-------+
Now we can create a fancy grid using the ‘fancy_grid’ argument as shown below:
print(tabulate(table,headers='firstrow',tablefmt='fancy_grid'))
Output:
╒══════════════╤═════════════╤═══════╕ │ First Name │ Last Name │ Age │ ╞══════════════╪═════════════╪═══════╡ │ Aayushi │ Agrawal │ 20 │ ├──────────────┼─────────────┼───────┤ │ Vivek │ Agrawal │ 50 │ ├──────────────┼─────────────┼───────┤ │ Archana │ Agrawal │ 48 │ ╘══════════════╧═════════════╧═══════╛
Similarly, we can create the same table using a dictionary. As you can see the results came out as expected. I hope you like it. If you have any doubts then please comment below.
I can’t say THANK YOU enough!
I’ve spent the last 4 days trying to get a list under the header (a special thank you also for the fancy grid)