Because Python is one of the world’s most popular programming languages, it’s the go-to option for plenty of coders.
In this article, we’ll be going over all of the different ways you can use Python to make a table.
Don’t worry though, this is one of the less difficult things you can do with the language so even a coding newbie can get the hang of this in minutes.
Advertising links are marked with *. We receive a small commission on sales, nothing changes for you.
Method 1: The Tabulate Module
The first, and most straightforward method for creating a table in Python is to use the tabulate module.
This module is used to create a text-based table output within the program using given inputs.
To install the tabulate module, simply use this command:
pip install tabulate
Here is an example that shows how a table is created in Python using the tabulate module:
# import module
from tabulate import tabulate
# assign data
mydata = [
[“Nikhil”, “Delhi”],
[“Ravi”, “Kanpur”],
[“Manish”, “Ahmedabad”],
[“Prince”, “Bangalore”]
]
# create header
head = [“Name”, “City”]
# display table
print(tabulate(mydata, headers=head, tablefmt=”grid”))
With this example, your table will come out neatly, displaying the headers: ‘name’ and ‘city’, as well as the individual units of data in each slot.
This is one of the simplest and most effective methods for creating a table in Python but it does lack some more advanced features that you might want to use.
Different Kinds Of Tables You Can Make With Tabulate
Just like with any program, there are different types of tables that can be made with every kind of module.
Because Tabulate is such a versatile tool for creating tables, you can generate each of the following tables using it:
Simple Tables With Headers
The example we’ve looked at already displays how you can create a basic table with headers.
This is perfect for a simple data set that needs to be displayed clearly.
There’s nothing too fancy about it and it gets the job done so this is a great place to start if you’re just getting into the world of coding.
Here is another example:
#create data
data = [[“Chelsea”, 99],
[“Liverpool”, 96],
[“Spurs”, 94],
[“City”, 88]]
#define header names
col_names = [“Team”, “Points”]
#display table
print(tabulate(data, headers=col_names))
With this simple setup, you’ll have created a table with two rows of data and two simple headers above each.
Tables With Fancy Grids
If you want your data to be displayed in a slightly more aesthetically pleasing table, you might want to use this code to create a fancier table.
It will still display the same data as with the other methods we’ve looked at but the borders and formatting will look a little cleaner.
Here’s how you do it:
#create data
data = [[“Chelsea”, 99],
[“Liverpool”, 96],
[“Spurs”, 94],
[“City”, 88]]
#define header names
col_names = [“Team”, “Points”]
#display table
print(tabulate(data, headers=col_names, tablefmt=”fancy_grid”))
As you can see, there isn’t much of a difference in the way you input the data but the display format will end up looking cleaner for you.
The key is in the final line of code that determines how the table is displayed.
Tables With Index Columns
As well as having headers and aesthetically cleaner formatting, you might want your table to have index columns as well.
This is essentially another column to the side of your main set of data without a header.
Here’s an example of how it works:
#create data
data = [[“Chelsea”, 99],
[“Liverpool”, 96],
[“Spurs”, 94],
[“City”, 88]]
#define header names
col_names = [“Team”, “Points”]
#display table
print(tabulate(data, headers=col_names, tablefmt=”fancy_grid”, showindex=”always”))
In this case, you’ll be left with a table that displays the same data as with the previous examples, also with the nicer formatting.
However, in this case, a separate index column to the left of your data will display without a header.
Method 2: The PrettyTable Module
Just like with the tabulate module, PrettyTable is something that you can simply install using Python with this command:
Pip Install prettytable
With this module, you can create relational tables in Python. Here is an example of a table that can be created in Python using the PrettyTable module:
from prettytable import PrettyTable
# Specify the Column Names while initializing the Table
myTable = PrettyTable([“Student Name”, “Class”, “Section”, “Percentage”])
# Add rows
myTable.add_row([“John”, “2A”, “B”, “91.2 %”])
myTable.add_row([“James”, “2A”, “C”, “63.5 %”])
myTable.add_row([“Jessica”, “2A”, “A”, “90.23 %”])
myTable.add_row([“Joseph”, “2A”, “D”, “92.7 %”])
myTable.add_row([“Jack”, “2A”, “A”, “98.2 %”])
myTable.add_row([“Jackie”, “2A”, “B”, “88.1 %”])
myTable.add_row([“Jonny”, “2A”, “B”, “95.0 %”])
print(myTable)
As you can see, this module allows for more rows to be printed out in a nicer, more easily readable format.
However, there are still even more ways that you can create different kinds of tables in Python, each with a different set of characteristics.
Method 3: Using Texttable
The final method we’re looking at for creating a table using Python is a module called ‘texttable’.
With this module, you can control a few more specific details of your table, including the vertical/horizontal alignment, style of border, and type of data.
Here’s an example of how it works:
from texttable import Texttable
# texttable takes the first reocrd in the list as the column names
# of the table
l = [[“Name”, “Age”, “University”], [“Donna”, 21, “LUMS”], [“Allan”, 22, “FAST”], [“Kyla”, 23, “UET”]]
table = Texttable()
table.add_rows(l)
print(table.draw())
Of course, this is just one example of how ‘texttable’ can be used to generate a table.
However, there is a wide variety of ways to generate vastly different tables using this module.
In fact, texttable is such a versatile tool that it deserves its own guide just on how to use it!
Final Thoughts
As you can tell, there are plenty of different ways to create a table using the Python programming language.
If you’re just getting started in the world of coding, any of the above methods will get you on the way to creating your own beautiful tables in no time!
Advertising links are marked with *. We receive a small commission on sales, nothing changes for you.