How do you print nice tables in python?

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']]
  col 1  col 2  col 3  col 4
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!

How do you print a table format in Python?

Print Data in Tabular Format in Python.
Use the format[] Function to Print Data in Table Format in Python..
Use the tabulate Module to Print Data in Table Format in Python..
Use the pandas.DataFrame[] Function to Print Data in Table Format in Python..

How do you print a table in Python using the function?

Print a table in python.
tabulate. The tabulate package is the most widely used Python package for tabulating lists; it has many options for specifying headers and table format. ... .
PrettyTable. PrettyTable is a package that serves a similar purpose to the tabulate package. ... .
texttable..

How do you make tables in Python?

The easiest way to create tables in Python is to use tablulate[] function from the tabulate library..
To use this function, we must first install the library using pip: pip install tabulate..
We can then load the library: from tabulate import tabulate..

What is a pretty table?

PrettyTable is a Python library that is used to represent tabular data in visually appealing ASCII tables. It is quick and easy to use.

Chủ Đề