How to Create a Table in Python

How to Create a Table in Python [2024]

Last Updated: June 24, 2024By

It is pretty much normal to use tables in Python when dealing with analysing data and trying to quantify it, so it is easy and efficient to get insights from the data.

So, it’s essential to learn how to create tables in Python. Many people use loops to create tables, which is good for visualising, but from a data analytics point of view, it is better to tabulate data using Python.

Let’s get started,

How to Create a Table in Python

First, we have to check whether the tabulate module is installed on our PC or not. We can check it in the command prompt or terminal if it is not installed; we can install it using the command  ‘pip install tabulate’  in cmd or terminal. Once it’s installed, we can proceed with the coding part. Using the following code, we can import the tabulate module:

how to create a table in python steps

1. Creating the table

After importing the tabulate module, we have to generate the data for the table using data containers like lists, dictionaries, etc.

Example code:

How to Create a Table in Python step 1
I have created a simple 2D list with some data.

After creating the list, you can write the following code to create a table.

Code:

How to Create a Table in Python result

Let me explain the syntax depicted above. We imported the tabulate function from the tabulate module, and then we used the tabulate function to create the table.The arguments are the lists to contain data, the header to highlight the header row, and the tablefmt to specify the desired format.

There are many table formats, to name a few : Fancy_grid, mixed_grid, heavy_grid,psql etc.You can check it in the documentations also.

output:

How to Create a Table in Python output

We can also use dictionaries to store the data but there we have to use the keys as headers as shown below:

How to Create a Table in Python tabulate

2. Creating numpy and pandas tables using tabulate

First, install numpy and Pandas modules.

To create the dataframes using tabulate we have to import numpy and pandas modules as well.

The following code gives us tableform for numpy arrays,

How to Create a Table in Python table form

Finally we will discuss how to create dataframes using pandas.

For that we can use previous dictionary,

There is inbuilt data format in the dataframe,so there is no need to use the tabulate function

How to Create a Table in Python step 2

If we want to change the tableformat we can use the tabulate function as,

How to Create a Table in Python table format

Output:
How to Create a Table in Python output 2

Conclusion

The tabulate library in Python is primarily used for pretty-printing tabular data in various formats (like plain text, HTML, GitHub-flavored Markdown, etc.).

It is not used to create database tables, but rather to format and display data in a tabular format. First, you need to install the tabulate library if you haven’t already. Import the tabulate function from the tabulate library. Prepare the data you want to display in a table.

The data can be in various forms like a list of lists, list of dictionaries, or a list of tuples. Use the tabulate function to create a table from your data. You can specify the table format (e.g., “plain”, “grid”, “pipe”, “html”, etc.).

editor's pick

latest video

news via inbox

Nulla turp dis cursus. Integer liberos  euismod pretium faucibua

Leave A Comment