VOOZH about

URL: https://www.geeksforgeeks.org/python/introduction-to-python-tabulate-library/

⇱ Introduction to Python Tabulate Library - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Introduction to Python Tabulate Library

Last Updated : 23 Jul, 2025

The Python tabulate module is a library and a command-line utility that displays data in a visually appealing format (tabulate format). Whether working with lists, dictionaries, pandas DataFrames, or other forms of structured data, the tabulate module can convert raw data into well-formatted tables. The Python tabulate module supports various table formats, such as plain text, grid, and HTML.

In this article, we will explore the features, installation, and usage of the Python tabulate module and work through an example to demonstrate how it can be applied.

Introduction to Tabulate Library

The tabulate library is a Python library that provides a function to display data in various tabular formats. It can process data structures such as lists, lists of dictionaries, or pandas DataFrames and output them in formats like plain text, grid, or GitHub-flavored markdown tables. The library supports quick formatting and offers features to customize headers, align text, and handle missing data.

To install Tabulate, use the command prompt and run the following pip command:

pip install tabulate

Key Features of Tabulate Library

The following are the key features of the tabulate library:

  • Supports Various Data Structures: It can be used to convert lists, lists of lists, lists of dictionaries, and pandas DataFrames into tables.
  • Customizable Table Formats: The tabulate module supports various formats for tables, such as plain text, grid, markdown, HTML, and more.
  • Text Alignment: Allows text alignment for better readability.
  • Handling of Missing Data: Can handle and represent missing data efficiently.
  • Pandas Integration: The tabulate module easily integrates with pandas DataFrames for direct tabular output.
  • Minimal Code: Requires only a few lines of code to generate clean, readable tables.

Using Tabulate

1. Simple Table from a List of Lists

We can pass a list of lists (representing rows of data) to the tabulate() function. Here's how it works:

Output:

πŸ‘ Screenshot-2024-09-12-120719
Display list of list using tabulate

2. Table from a List of Dictionaries

We can also pass a list of dictionaries, where each dictionary represents a row of data, with the keys as the headers.

Output:

πŸ‘ Screenshot-2024-09-12-121633
Displaying table from list of dictionaries using Tabulate

Formatting Techniques in tabulate

The tabulate module offers various formatting options, such as table formats, text alignment, and the ability to customize headers. In this section, we will explore these formats and integrate tabulate with pandas DataFrames.

1. Table Formats

tabulate supports various formats, which can be specified using the tablefmt parameter. Some common formats include:

  • plain: Simple table with no borders or padding.
  • grid: Adds grid lines around each cell.
  • pipe: GitHub Markdown-style tables.
  • html: Output as an HTML table.
  • rst: Generates tables compatible with reStructuredText (reST), which is widely used for technical documentation, including Python's documentation.

Example using the pipe format:

Output:

πŸ‘ Screenshot-2024-09-12-122233
using pipe format in Tabulate

Example using the rst format:

Output:

πŸ‘ Screenshot-2024-09-12-140752
using rst format in Tabulate

2. Aligning Text

In the Tabulate, we can control the alignment of text using the colalign or align parameters. Here’s the syntax for that:

The alignment in the above code is as follows:

  • First column (Name) is left-aligned.
  • Second column (Age) is right-aligned.
  • Third column (Occupation) is center-aligned.

Example using colalilgn:

Output:

πŸ‘ Screenshot-2024-09-12-132406
Using colalign

3. Customizing Headers

In the Tabulate module, by default, the headers are inferred from the data. However, we can also pass our own custom headers to the table using the headers parameter:

Output:

πŸ‘ Screenshot-2024-09-12-133604
Customizing headers in Tabulate

4. Working with Tabulate and Pandas DataFrames

Tabulate integrates well with pandas, allowing us to display DataFrames as tables. Here's how we can convert a pandas DataFrame into a tabulated table:

Output:

πŸ‘ Screenshot-2024-09-12-133713
Working with Tabulate and Pandas

5. Handling Missing Data

The Tabulate module can handle missing values effectively. If a value is missing (None or NaN in pandas), Tabulate has a parameter missingval that we can define, such as 'N/A', and it will replace the missing value.

Here’s an example showing that:

Output:

πŸ‘ Screenshot-2024-09-12-134713
Handling Missing Values in Tabulate

Conclusion

In this article, we learned about the Python tabulate module. This Python library is a simple yet powerful tool for creating well-formatted tables from Python data structures. We explored its features, uses, and solved some examples. The tabulate module supports many tabular formats, such as grid, plain, HTML, and others. With features like text alignment, customizable headers, and the ability to handle missing data, tabulate offers an easy way to display data in an organized manner with minimal code.

Comment
Article Tags: