To create a table from python you would use

How to use the tabulate function to create nicely-formatted tables in Python

To create a table from python you would use

Photo by Fotis Fotopoulos on Unsplash

Being able to quickly organize our data into a more readable format, such as when data wrangling, can be extremely helpful in order to analyze the data and plan the next steps. Python offers the ability to easily turn certain tabular data types into nicely formatted plain-text tables, and that’s with the tabulate function.

install tabulate

We first install the tabulate library using pip install in the command line:

pip install tabulate

import tabulate function

We then import the tabulate function from the tabulate library in our code:

from tabulate import tabulate

And now we are ready to use the tabulate function!

tabular data types supported by tabulate

The tabulate function can transform any of the following into an easy to read plain-text table: (from the tabulate documentation)

  • list of lists or another iterable of iterables
  • list or another iterable of dicts (keys as columns)
  • dict of iterables (keys as columns)
  • two-dimensional NumPy array
  • NumPy record arrays (names as columns)
  • pandas.DataFrame

list of lists

For example, if we have the following list of lists:

table = [['First Name', 'Last Name', 'Age'], ['John', 'Smith', 39], ['Mary', 'Jane', 25], ['Jennifer', 'Doe', 28]]

We can turn it into into a much more readable plain-text table using the tabulate function:

print(tabulate(table))

To create a table from python you would use

Since the first list in the list of lists contains the names of columns as its elements, we can set it as the column or header names by passing ‘firstrow’ as the argument for the headers parameter:

print(tabulate(table, headers='firstrow'))

To create a table from python you would use

The tabulate function also contains a tablefmt parameter, which allows us to improve the appearance of our table using pseudo-graphics:

print(tabulate(table, headers='firstrow', tablefmt='grid'))

To create a table from python you would use

I prefer to use the ‘fancy_grid’ argument for tablefmt:

print(tabulate(table, headers='firstrow', tablefmt='fancy_grid'))

To create a table from python you would use

dictionary of iterables

We can create the same table above using a dictionary:

info = {'First Name': ['John', 'Mary', 'Jennifer'], 'Last Name': ['Smith', 'Jane', 'Doe'], 'Age': [39, 25, 28]}

In the case of a dictionary, the keys will be the column headers, and the values will be the elements of those columns. We specify that the keys will be the headers by passing ‘keys’ as the argument for the headers parameter:

print(tabulate(info, headers='keys'))

To create a table from python you would use

And of course we can use the tablefmt parameter to improve the table’s appearance:

print(tabulate(info, headers='keys', tablefmt='fancy_grid'))

To create a table from python you would use

adding an index

We can also add an index to our table with the showindex parameter:

To create a table from python you would use

We can add a custom index by passing in an iterable to the showindex parameter. For example, if we want the index to start at 1, we can pass in a range object as the argument:

To create a table from python you would use

missing values

If we remove ‘Jennifer’ from the above info dictionary, our table will contain an empty field:

print(tabulate({'First Name': ['John', 'Mary'], 'Last Name': ['Smith', 'Jane', 'Doe'], 'Age': [39, 25, 28]}, headers="keys", tablefmt='fancy_grid'))

To create a table from python you would use

If there any missing values in our table, we can choose what to fill them in with using the missingval parameter. The default value for missingval is an empty string. If we change it to ‘N/A’, this is what what our table will look like:

print(tabulate({'First Name': ['John', 'Mary'], 'Last Name': ['Smith', 'Jane', 'Doe'], 'Age': [39, 25, 28]}, headers="keys", tablefmt='fancy_grid', missingval='N/A'))

To create a table from python you would use

I hope this tutorial on how to easily create nicely formatted tables using the tabulate function was helpful. Thank you for reading!

Which command is used to create a table in Python?

Creating a Table. To create a table in MySQL, use the "CREATE TABLE" statement.

How do I create a text table in Python?

Your text-file needs a fixed structure (for example all values are separated by a tabulate or a line break). Then you can use the pd. read_csv method and define the separator by hand with pd. read_csv('yourFileName', sep='yourseperator') .

What is a table in Python called?

Anvil represents Data Tables in code as Python objects.

How use SQL table in Python?

After connecting to the database and creating the cursor object let's see how to execute the queries. And executing the command is very easy. Call the cursor method execute() and pass the name of the sql command as a parameter in it. Save a number of commands as the sql_comm and execute them.