How do you make a table in python?

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

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]]

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!

Which command is used to create a table in Python?

To create a table using python you need to execute the CREATE TABLE statement using the execute[] method of the Cursor of pyscopg2.

How do you create the table?

For a basic table, click Insert > Table and move the cursor over the grid until you highlight the number of columns and rows you want. For a larger table, or to customize a table, select Insert > Table > Insert Table.

How do you display data in a table 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 I convert a CSV file to a table in Python?

Using Python to Write a Create Table Statement and Load a CSV into Redshift.
Importing Libraries and Reading Data in Python. The first step is to load the data, import libraries, and load the data into a CSV reader object. ... .
Finding the Data Type. ... .
Finishing the Job..

Chủ Đề