How to Create Tables in Python (With Examples)


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

We can then use the following basic syntax to create tables:

print(tabulate(data, headers=col_names, tablefmt="grid", showindex="always"))

The following examples show how to use this function in practice.

Example 1: Create Table with Headers

The following code shows how to create a basic table with headers:

#create data
data = [["Mavs", 99], 
        ["Suns", 91], 
        ["Spurs", 94], 
        ["Nets", 88]]
  
#define header names
col_names = ["Team", "Points"]
  
#display table
print(tabulate(data, headers=col_names))

Team      Points
------  --------
Mavs          99
Suns          91
Spurs         94
Nets          88

Example 2: Create Table with Fancy Grid

The following code shows how to create a table with headers and a fancy grid:

#create data
data = [["Mavs", 99], 
        ["Suns", 91], 
        ["Spurs", 94], 
        ["Nets", 88]]
  
#define header names
col_names = ["Team", "Points"]
  
#display table
print(tabulate(data, headers=col_names, tablefmt="fancy_grid"))

╒════════╤══════════╕
│ Team   │   Points │
╞════════╪══════════╡
│ Mavs   │       99 │
├────────┼──────────┤
│ Suns   │       91 │
├────────┼──────────┤
│ Spurs  │       94 │
├────────┼──────────┤
│ Nets   │       88 │
╘════════╧══════════╛

Note that the tablefmt argument accepts several different options including:

  • grid
  • fancy_grid
  • pipe
  • pretty
  • simple

Refer to the tabulate documentation for a complete list of potential table formats.

Example 3: Create Table with Index Column

The following code shows how to create a table with headers, a fancy grid, and an index column:

#create data
data = [["Mavs", 99], 
        ["Suns", 91], 
        ["Spurs", 94], 
        ["Nets", 88]]
  
#define header names
col_names = ["Team", "Points"]
  
#display table
print(tabulate(data, headers=col_names, tablefmt="fancy_grid", showindex="always"))

╒════╤════════╤══════════╕
│    │ Team   │   Points │
╞════╪════════╪══════════╡
│  0 │ Mavs   │       99 │
├────┼────────┼──────────┤
│  1 │ Suns   │       91 │
├────┼────────┼──────────┤
│  2 │ Spurs  │       94 │
├────┼────────┼──────────┤
│  3 │ Nets   │       88 │
╘════╧════════╧══════════╛

Example 4: Create Table with HTML Format

The following code shows how to create a table in HTML format, which is useful for web applications:

#create data
data = [["Mavs", 99], 
        ["Suns", 91], 
        ["Spurs", 94], 
        ["Nets", 88]]
  
#define header names
col_names = ["Team", "Points"]
  
#display table in HTML format
print(tabulate(data, headers=col_names, tablefmt="html"))

<table>
<thead>
<tr><th>Team</th><th>Points</th></tr>
</thead>
<tbody>
<tr><td>Mavs</td><td>99</td></tr>
<tr><td>Suns</td><td>91</td></tr>
<tr><td>Spurs</td><td>94</td></tr>
<tr><td>Nets</td><td>88</td></tr>
</tbody>
</table>

This format is particularly useful when you need to integrate the table into web pages or web apps built with frameworks like Streamlit, Dash, or Flask. 

Example 5: Create Table with Custom Column Alignment

The following code shows how to create a table with custom column alignment:

#create data
data = [["Mavs", 99], 
        ["Suns", 91], 
        ["Spurs", 94], 
        ["Nets", 88]]
  
#define header names
col_names = ["Team", "Points"]
  
#display table with custom column alignment
print(tabulate(data, headers=col_names, tablefmt="grid", colalign=("left", "center")))

+--------+----------+
| Team   |  Points  |
+========+==========+
| Mavs   |    99    |
+--------+----------+
| Suns   |    91    |
+--------+----------+
| Spurs  |    94    |
+--------+----------+
| Nets   |    88    |
+--------+----------+

The colalign parameter accepts a tuple with alignment values for each column. The options include “left”, “right”, and “center”.

Wrapping Up

The tabulate library provides a simple and effective way to create formatted tables in Python. Here’s a summary of what we’ve covered:

  • Creating basic tables with headers
  • Adding fancy grid formatting
  • Including index columns
  • Generating HTML formatted tables
  • Customizing column alignment

These techniques allow you to present data in a structured and readable format. The tabulate library is particularly useful for displaying data in terminal outputs, creating documentation, or preparing results for presentation.

For more advanced table formatting, consider using pandas DataFrames with the .to_string() method or specialized libraries like PrettyTable. However, for most use cases, the tabulate library offers the right balance of simplicity and flexibility.

2 Replies to “How to Create Tables in Python (With Examples)”

    1. Hi Pep,

      Thanks for your comment! You’ve highlighted an important point about the tabulate library.

      You’re absolutely right that if you import the library using import tabulate, you would need to call tabulate.tabulate() to create your tables.

      In the article, we’re using a different import approach with from tabulate import tabulate, which brings the function directly into the namespace. This is why we can simply call tabulate() without the library prefix.

      Both methods work perfectly fine:
      # Method 1 (as shown in the article)
      from tabulate import tabulate
      print(tabulate(data, headers=col_names))

      # Method 2 (as you mentioned)
      import tabulate
      print(tabulate.tabulate(data, headers=col_names))

      Thanks for pointing this out, as it helps readers who might be using different import styles!

Leave a Reply

Your email address will not be published. Required fields are marked *