Hello fellow coders! Today in this tutorial, you are going to learn how to create tables in Python with the help of the tabulate
function and learn about various properties involved with the tables created.
Steps to use the tabulate module to create tables in Python
Without any further ado, let’s get right into the steps to create tables in Python with the use of the tabulate module.
1. Importing tabulate
The first step is to import the tabulate function from the tabulate library. In case this results in an error make sure you have the tabulate library installed by executing the pip install
command on the command prompt.
from tabulate import tabulate
Now let’s create our very first table with the help of the tabulate function.
2. Creating Simple Tables
The data of the table is stored in the form of nested lists as shown in the code mentioned below.
all_data = [["Roll Number","Student name","Marks"],
[1,"Sasha",34],
[2,"Richard",36],
[3,"Judy",20],
[4,"Lori",39],
[5,"Maggie",40]]
To tabulate the data, we just pass the data to the tabulate
function. We can also make the first nested list as the head of the table by using an attribute known as headers
.
table1 = tabulate(all_data)
table2 = tabulate(all_data,headers='firstrow')
The results of both the tables are shown below.
----------- ------------ -----
Roll Number Student name Marks
1 Sasha 34
2 Richard 36
3 Judy 20
4 Lori 39
5 Maggie 40
----------- ------------ -----
Roll Number Student name Marks
------------- -------------- -------
1 Sasha 34
2 Richard 36
3 Judy 20
4 Lori 39
5 Maggie 40
3. Formatting the Python table to make it look better
To make tables in Python look better, we can add borders for the table to make it look more tabular instead of textual data. The borders can be added with the help of the tablefmt
attribute and set its value to grid
.
print(tabulate(all_data,headers='firstrow',tablefmt='grid'))
+---------------+----------------+---------+
| Roll Number | Student name | Marks |
+===============+================+=========+
| 1 | Sasha | 34 |
+---------------+----------------+---------+
| 2 | Richard | 36 |
+---------------+----------------+---------+
| 3 | Judy | 20 |
+---------------+----------------+---------+
| 4 | Lori | 39 |
+---------------+----------------+---------+
| 5 | Maggie | 40 |
+---------------+----------------+---------+
To make it look better, we can use fancy_grid
instead of a simple grid.
print(tabulate(all_data,headers='firstrow',tablefmt='fancy_grid'))
╒═══════════════╤════════════════╤═════════╕
│ Roll Number │ Student name │ Marks │
╞═══════════════╪════════════════╪═════════╡
│ 1 │ Sasha │ 34 │
├───────────────┼────────────────┼─────────┤
│ 2 │ Richard │ 36 │
├───────────────┼────────────────┼─────────┤
│ 3 │ Judy │ 20 │
├───────────────┼────────────────┼─────────┤
│ 4 │ Lori │ 39 │
├───────────────┼────────────────┼─────────┤
│ 5 │ Maggie │ 40 │
╘═══════════════╧════════════════╧═════════╛
4. Extracting HTML code of the table from tabulate
To extract the HTML code of the table, we need to set the tablefmt
attribute to html
. The same is displayed below.
print(tabulate(all_data,headers='firstrow',tablefmt='html'))
<table>
<thead>
<tr><th style="text-align: right;"> Roll Number</th><th>Student name </th><th style="text-align: right;"> Marks</th></tr>
</thead>
<tbody>
<tr><td style="text-align: right;"> 1</td><td>Sasha </td><td style="text-align: right;"> 34</td></tr>
<tr><td style="text-align: right;"> 2</td><td>Richard </td><td style="text-align: right;"> 36</td></tr>
<tr><td style="text-align: right;"> 3</td><td>Judy </td><td style="text-align: right;"> 20</td></tr>
<tr><td style="text-align: right;"> 4</td><td>Lori </td><td style="text-align: right;"> 39</td></tr>
<tr><td style="text-align: right;"> 5</td><td>Maggie </td><td style="text-align: right;"> 40</td></tr>
</tbody>
</table>
Conclusion
In this tutorial, we created our own tabular data using the tabulate
function and also learned about some properties of the tables. Hope you liked it!
Thank you for reading!