banner



How To Create A Data Table In Python

How to Easily Create Tables in Python

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

Luay Matalka

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 tabula t e 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))          

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'))          

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'))          

I prefer to use the 'fancy_grid' argument for tablefmt :

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

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'))          

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

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

adding an index

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

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:

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'))          

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'))          

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

How To Create A Data Table In Python

Source: https://towardsdatascience.com/how-to-easily-create-tables-in-python-2eaea447d8fd

Posted by: mezadogese.blogspot.com

0 Response to "How To Create A Data Table In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel