Table Output with PrettyTable Library in Python

Table Output with PrettyTable Library in Python

In this article, we will look at how to create good-looking and simplified tables in Python using the PrettyTable library or module. We will see all the methods that are used for creating and deleting rows and columns with examples and will go deep into all the methods for better understanding. But before that, let’s see a brief introduction to Creating Tables Using PrettyTable library in Python.

PrettyTable Library in Python

PrettyTable Library is an open-source library in python. We use this library to make good relational tables and to do so we use the PrettyTable class inside the prettytable library. The latest version of the PrettyTable library is 3.3.0.

Installing PrettyTable Library

pip install prettytable

Creating the table row-wise

from prettytable import PrettyTable

# Calling PrettyTable class and specifying the column names
my_table = PrettyTable(["City Name", "Area", "Population", "Annual Rainfall"])


my_table.add_row(["Chennai", 426, 10971108, 1400])
my_table.add_row(["Agra", 121, 2314000, 245])
my_table.add_row(["New Delhi", 42.7, 30291000, 653.6])
my_table.add_row(["Mumbai", 603.4, 9356962, 95.35])
my_table.add_row(["Kolkata", 206.1, 14900000, 72.30])
my_table.add_row(["Jaipur", 467, 4107000, 25.02])


print(my_table)

Output:

+-----------+-------+------------+-----------------+
| City Name |  Area | Population | Annual Rainfall |
+-----------+-------+------------+-----------------+
|  Chennai  |  426  |  10971108  |       1400      |
|    Agra   |  121  |  2314000   |       245       |
| New Delhi |  42.7 |  30291000  |      653.6      |
|   Mumbai  | 603.4 |  9356962   |      95.35      |
|  Kolkata  | 206.1 |  14900000  |       72.3      |
|   Jaipur  |  467  |  4107000   |      25.02      |
+-----------+-------+------------+-----------------+

Creating the table column-wise

from prettytable import PrettyTable

# Calling PrettyTable class and specifying the column names
my_table = PrettyTable()


# Adding Column Data
my_table.add_column("City name",["Chennai", "Agra", "New Delhi", "Mumbai", "Kolkata", "Jaipur"])
my_table.add_column("Area", [426, 121, 42.7, 603.4, 206.1, 467])
my_table.add_column("Population", [10971108, 2314000, 30291000, 9356962, 14900000, 4107000])
my_table.add_column("Annual Rainfall",[1400, 245, 653.6, 95.35, 72.30, 25.02])


print(my_table)

Output:

+-----------+-------+------------+-----------------+
| City name |  Area | Population | Annual Rainfall |
+-----------+-------+------------+-----------------+
|  Chennai  |  426  |  10971108  |       1400      |
|    Agra   |  121  |  2314000   |       245       |
| New Delhi |  42.7 |  30291000  |      653.6      |
|   Mumbai  | 603.4 |  9356962   |      95.35      |
|  Kolkata  | 206.1 |  14900000  |       72.3      |
|   Jaipur  |  467  |  4107000   |      25.02      |
+-----------+-------+------------+-----------------+

Deleting Row and Column of a Table

1. Delete Row

# This will delete row at index 0
my_table.del_row(0)

2. Delete Column

# This will delete the column with name Area
my_table.del_column("Area")

Clearing the Table

For deleting all the rows of our table we have to call clear_rows() of PrettyTable class, this function will not delete the column name.

# This will only delete all the rows of our table
my_table.clear_rows()

Delete Complete Table

# This will delete all the rows and columns of our table
my_table.clear()

Getting Data of Particular Rows and Columns of a Table

Getting Data of Particular Columns of a Table

from prettytable import PrettyTable

# Calling PrettyTable class and specifying the column names
my_table = PrettyTable()


# Adding Column Data
my_table.add_column("City name", ["Chennai", "Agra", "New Delhi", "Mumbai", "Kolkata", "Jaipur"])
my_table.add_column("Area", [426, 121, 42.7, 603.4, 206.1, 467])
my_table.add_column("Population", [10971108, 2314000, 30291000, 9356962, 14900000, 4107000])
my_table.add_column("Annual Rainfall", [1400, 245, 653.6, 95.35, 72.30, 25.02])


print(my_table.get_string(fields=["City name", "Annual Rainfall"]))

Output:

+-----------+-----------------+
| City name | Annual Rainfall |
+-----------+-----------------+
|  Chennai  |       1400      |
|    Agra   |       245       |
| New Delhi |      653.6      |
|   Mumbai  |      95.35      |
|  Kolkata  |       72.3      |
|   Jaipur  |      25.02      |
+-----------+-----------------+

Here, inside print() function we have called get_string() function of PrettyTable class and as an input parameter we have passed fields = [“City Name”, “Annual Rainfall”]. So, we will get the output table with only 2 columns “City Name” and “Annual Rainfall”.

Getting Data of Particular Rows of a Table

from prettytable import PrettyTable

# Calling PrettyTable class and specifying the column names
my_table = PrettyTable()


# Adding Column Data
my_table.add_column("City name", ["Chennai", "Agra", "New Delhi", "Mumbai", "Kolkata", "Jaipur"])
my_table.add_column("Area", [426, 121, 42.7, 603.4, 206.1, 467])
my_table.add_column("Population", [10971108, 2314000, 30291000, 9356962, 14900000, 4107000])
my_table.add_column("Annual Rainfall", [1400, 245, 653.6, 95.35, 72.30, 25.02])

print(my_table.get_string(start = 1, end = 4))

Output:

+-----------+-------+------------+-----------------+
| City name |  Area | Population | Annual Rainfall |
+-----------+-------+------------+-----------------+
|    Agra   |  121  |  2314000   |       245       |
| New Delhi |  42.7 |  30291000  |      653.6      |
|   Mumbai  | 603.4 |  9356962   |      95.35      |
+-----------+-------+------------+-----------------+

In the above code as an input parameter of get_string() we have passed start = 1, end = 3. So, we will get rows from index 1 to index 3.

Changing the alignment of columns

We can easily align the column data in different directions, to do so we have to pass one character string to align the attribute of PrettyTable. The one-character Strings are as follows-

  • “l”– used for left side alignment.
  • “r” used for right side alignment.
  • “c”– is used for center alignment.

Only these one-character strings are valid. By Default alignment, PrettyTable is the center.

from prettytable import PrettyTable

# Calling PrettyTable class and specifying the column names
my_table = PrettyTable()


# Adding Column Data
my_table.add_column("City name", ["Chennai", "Agra", "New Delhi", "Mumbai", "Kolkata", "Jaipur"])
my_table.add_column("Area", [426, 121, 42.7, 603.4, 206.1, 467])
my_table.add_column("Population", [10971108, 2314000, 30291000, 9356962, 14900000, 4107000])
my_table.add_column("Annual Rainfall", [1400, 245, 653.6, 95.35, 72.30, 25.02])

my_table.align = "r" 

print(my_table)

Output:

+-----------+-------+------------+-----------------+
| City name |  Area | Population | Annual Rainfall |
+-----------+-------+------------+-----------------+
|   Chennai |   426 |   10971108 |            1400 |
|      Agra |   121 |    2314000 |             245 |
| New Delhi |  42.7 |   30291000 |           653.6 |
|    Mumbai | 603.4 |    9356962 |           95.35 |
|   Kolkata | 206.1 |   14900000 |            72.3 |
|    Jaipur |   467 |    4107000 |           25.02 |
+-----------+-------+------------+-----------------+

We hope this article on PrettyTable in Python helps you.

Thank you for reading this article, click here to start learning Python in 2022.


Also Read:

Share:

Author: Ayush Purawr