How to Create a Table in Python?

Python has a large number of libraries that can be used for a variety of purposes. In this article, we’ll look at two such methods that can be used to make tables.

1. Using the tabulate module

2. Using the PrettyTable module

1. Using the tabulate module:

In this module, we have Tabulate () method that is used to create a text-based table. This module is used for the function that includes:

printing small-sized tables without formatting tools and requires only one function call. This module helps in easily understanding how to frame a table.

• It helps in composing tabular data for lightweight plain-text markup.

• It provides a clear presentation of different numeric data and textual.

The tabulate library is installed using pip install tabulate command, or we can install it directly from the settings of the Pycharm IDE. The method used in tabulate module is tabulate().

Example 1:

from tabulate import tabulatemydata = [["Suman", "Lahore"],["Sara", "Karachi"],["Nisha", "Ahmedabad"],["Neha", "Islamabad"], ]head = ["Name","City"]print(tabulate(mydata, headers=head)


In example 1, we have installed the tabulate library and then import tabulate function from the tabulate library. Our data is a list of lists that is passed to the tabulate() method. We have a parameter header that accepts two string values: ‘Name’ and ‘City’ will be the column titles. This tabulate() function will arrange the data in a tabular format automatically as shown in figure 2.

Example 2:

from tabulate import tabulatemydata = [["Suman", "Lahore"],["Sara", "Karachi"],["Nisha", "Ahmedabad"],["Neha", "Islamabad"], ]head = ["Name","City"]print(tabulate(mydata, headers=head,tablefmt="fancy_grid")


In example 2, we have used another parameter of tabulate() function which is table format. This design and look of the table are improved using the fancy_grid displayed in figure 4.

Example 3:

from tabulate import tabulatemydata = [["Suman", "Lahore"],["Sara", "Karachi"],["Nisha", "Ahmedabad"],["Neha", "Islamabad"], ]head = ["Name","City"]print(tabulate(mydata, headers=head,tablefmt="fancy_grid",showindex="always")


In example 3, we have added an index column to the table using the “showindex parameter. A new index column will be added to the left displayed in figure 6.

2. Using the PrettyTable module

The PrettyTable is another Python library that helps in creating simple Relational tables. This library allows you to adjust various characteristics of a table, such as text alignment, column padding width, and table border.

This command is used to install it: pip install prettytable

We can create a table using the prettytable module in python:

1. Row-Wise

2. Column-Wise

1.Row-Wise Creation of Table

We create a table by inserting the values row-wise and for that purpose, we use the syntax:



from prettytable import PrettyTablemyTable = PrettyTable(["First Name", "L.Name", "City", "Roll no","Percentage"])myTable.add_row(["Suman", "Islam", "Lahore","086", "91.2 %"])myTable.add_row(["Nida", "Noor", "Islamabad","002", "63.5 %"])myTable.add_row(["Noor", "Khan", "Rawalpindi","012", "90.23 %"])myTable.add_row(["Sami", "John","Islamabad", "001", "92.7 %"])print(myTable)


2. Column-Wise Creation of Table

we create a table by inserting the values column-wise and for that we use the syntax:



from prettytable import PrettyTablecolumns = ["Student Name", "Class", "Section", "Percentage"]myTable = PrettyTable()myTable.add_column(columns[0], ["Ali", "Zoya", "Hamza","Kanwal", "John", "Katty", "Beni"])myTable.add_column(columns[1], ["XI", "XII", "XI", "XII", "XI", "XII", "XI"])myTable.add_column(columns[2], ["C", "C", "B", "D", "A", "B", "B"])myTable.add_column(columns[3], ["84.2 %", "90.5 %", "86.23 %", "92.7 %", "98.2 %", "88.1 %", "95.0 %"])print(myTable)


Print Friendly, PDF & Email