Do you want to make your tabular data look nice in Python? There are some useful libraries to get the job done.
In this article, we'll show you some helpful libraries to print and format a table in Python quickly, easily, and in a visually appealing way – that is, pretty printing. With little effort, your tables will be ready for an online publication, an analytics report, or a scientific paper.
Python has emerged as one of the go-to languages for data analysis. It is powerful and flexible. Its clear and easy-to-understand syntax makes it a great language to learn, even for beginners. The huge number of open-source libraries provide functionality for everything from scraping, cleaning, and manipulating data, to visualization and machine learning.
This article is aimed at more experienced programmers and data analysts. If you're a beginner, here's a great course that gets you on your feet.
Let's start by taking a look at some quick and dirty methods to print tables in Python for those times when you're in a hurry.
Not-So-Pretty Printing
During the exploratory data analysis phase, you're right to not worry too much about aesthetics. It doesn't make sense to waste your time producing nice-looking graphs and tables. Instead, you're just interested in understanding the data.
There are some quick techniques to print a table in Python. The first is string formatting with the format[] method. Let's say you have some tabular data stored in a list of lists. This can be quickly printed row-for-row as shown below:
table = [[1, 2222, 30, 500], [4, 55, 6777, 1]] for row in table: print['| {:1} | {:^4} | {:>4} | {:>> print[tabulate[table, headers='firstrow', tablefmt='html']]
1 | 2222 | 30 | 500 |
4 | 55 | 6777 | 1 |
prettytable
The prettytable library provides an alternative solution with some unique functionality. We'll use the PrettyTable[]
class to define, modify, and print tables in Python.
Here's how to define a table object with the header information, and then add multiple rows at once using the add_rows[]
method:
from prettytable import PrettyTable table = [['col 1', 'col 2', 'col 3', 'col 4'], [1, 2222, 30, 500], [4, 55, 6777, 1]] tab = PrettyTable[table[0]] tab.add_rows[table[1:]]
From here, you can simply print[]
the table to visualize it in ASCII form, or you can use the many available
methods to modify and format tabular data. To add a single row, there’s the add_row[]
method; to add a column, use the add_column[]
method. The latter has two required arguments: a string to define fieldname
and a list or tuple as column. You can also define the horizontal and vertical alignments as shown in the following example:
tab.add_column['col 5', [-123, 43], align='r', valign='t'] print[tab]
In many
cases, you have your tabular data saved in a CSV file or a database. The prettytable
library comes with the functionality to read in data from an external source such as a CSV, as shown below:
from prettytable import from_csv with open['data_file.csv'] as table_file: tab = from_csv[table_file]
For databases with a Python library that conforms to the Python DB-API – an SQLite database, for example – you can define a cursor object then build a table using the from_db_cursor[]
function from prettytable
. To do this, you only need about 4 lines of Python code.
One advantage of this library is
the ability to modify tabular data. Another is the additional functionality that gives you control over what data to display from the table. Using the get_string[]
method with the fields
argument allows you to control which columns are displayed. Similarly, the start
and end arguments allow you to define the indexes of the rows you want to display. This method also contains the sortby
keyword, which allows you to sort your tabular data by a particular column.
Like the tabulate
library,
the prettytable
library also comes with pre-defined formats to help publish tables in different ways. You can publish in a Microsoft-Word-friendly style, for example, and there are formats for JSON and HTML with customization options. If you are interested in learning how to process data in different file formats including CSV and JSON, take a look at this course.
If you want more fine-grained control over displaying tabular data, you can also specify properties manually. Let's take a look at a more complex example of configuring tabular data in Python:
from prettytable import ALL, FRAME tab = PrettyTable[table[0]] tab.add_rows[table[1:]] tab.hrules = ALL tab.vrules = FRAME tab.int_format = '8' tab.padding_width = 2 tab.junction_char = '.' tab.sortby = 'col 2' print[tab]
Closing Thoughts on Pretty Printing Tabular Data in Python
We have explored various ways of displaying tabular data in Python. Whether you're looking for a quick and dirty representation to help understand your data or preparing your table for publication online or in a scientific journal, the methods discussed here provide you with the tools to get started.
But there's always more to discover than what we can cover in an article. We encourage you to experiment with the code snippets and start building a nice visualization of your tabular data in Python.
If you're looking for more material on using Python for data science, check out this course. It includes useful lessons and exercises to send you on your way toward becoming a better data scientist. Happy coding!