An easy way to display tables using Tabulate in Pyhton
Python is a popular and versatile programming language often used for data analysis, and people commonly use it to present data in tabular form. For example, if we want to display data in a List, the data would be easier to understand and comprehend if we state it in a table. There are several ways to display a table, whether using a standard method or a library like Tabulate.
Using a standard method to display table
In this article, we use data related to clubs in the Premier League, which is structured as a List.
premier_league = [
["Club", "Played", "Won", "Drawn", "Lost", "GF", "GA", "GD", "Points"],
["Manchester City", 38, 28, 5, 5, 94, 33, 61, 89],
["Arsenal", 38, 26, 6, 6, 88, 43, 45, 84],
["Manchester United", 38, 23, 6, 9, 58, 43, 15, 75],
["Newcastle United", 38, 19, 14, 5, 68, 33, 35, 71],
["Liverpool", 38, 19, 10, 9, 75, 47, 28, 67],
["Brighton And Hove Albion", 38, 18, 8, 12, 72, 53, 19, 62],
["Aston Villa", 38, 18, 7, 13, 51, 46, 5, 61],
["Tottenham Hotspur", 38, 18, 6, 14, 70, 63, 7, 60],
["Brentford", 38, 15, 14, 9, 58, 46, 12, 59],
["Fulham", 38, 15, 7, 16, 55, 53, 2, 52]
]
col_widths = [max(len(str(item)) for item in col) for col in zip(*premier_league)]
for row in premier_league:
row_str = ""
for i, item in enumerate(row):
row_str += str(item).ljust(col_widths[i] + 2)
print(row_str)
We use List Comprehension to calculate the maximum width for each column. The zip(*premier_league) function is used to access each column in the premier_league List, and then we find the entire length of each item in that column.
The first loop iterates through each row in premier_league, including the table header and club data. Then, the second loop iterates through each element (item) in each row. In this loop, we build each row of the table by concatenating the items in the row and ensuring each column has the appropriate width. Once the entire row has been built in row_str, the row is printed to the screen. Here is the output generated from the Python code above:
### Output
Club Played Won Drawn Lost GF GA GD Points
Manchester City 38 28 5 5 94 33 61 89
Arsenal 38 26 6 6 88 43 45 84
Manchester United 38 23 6 9 58 43 15 75
Newcastle United 38 19 14 5 68 33 35 71
Liverpool 38 19 10 9 75 47 28 67
Brighton And Hove Albion 38 18 8 12 72 53 19 62
Aston Villa 38 18 7 13 51 46 5 61
Tottenham Hotspur 38 18 6 14 70 63 7 60
Brentford 38 15 14 9 58 46 12 59
Fulham 38 15 7 16 55 53 2 52
What is Tabulate? And how to use it?
Tabulate is a Python library that creates tables in text format from existing data. With Tabulate, you can easily create tables in Python code and print them to the console or save them in text format. The following are the general steps to use Tabulate:
- The first step is to install the Tabulate library. If you haven’t installed it yet, you can do so with the following pip command:
pip install tabulate
- In your Python code, import the Tabulate module by using an import statement like this:
from tabulate import tabulate
- Using the premier_league List above, we can easily create a table using the tabulate() function:
print(tabulate(premier_league, headers = "firstrow", tablefmt = "rst"))
### Output
======================== ======== ===== ======= ====== ==== ==== ==== ========
Club Played Won Drawn Lost GF GA GD Points
======================== ======== ===== ======= ====== ==== ==== ==== ========
Manchester City 38 28 5 5 94 33 61 89
Arsenal 38 26 6 6 88 43 45 84
Manchester United 38 23 6 9 58 43 15 75
Newcastle United 38 19 14 5 68 33 35 71
Liverpool 38 19 10 9 75 47 28 67
Brighton And Hove Albion 38 18 8 12 72 53 19 62
Aston Villa 38 18 7 13 51 46 5 61
Tottenham Hotspur 38 18 6 14 70 63 7 60
Brentford 38 15 14 9 58 46 12 59
Fulham 38 15 7 16 55 53 2 52
======================== ======== ===== ======= ====== ==== ==== ==== ========
You can also customize the appearance of the table by combining multiple parameters. For example, you can set the table format and column alignment. Here is an example:
print(tabulate(premier_league, headers = "firstrow", tablefmt = "fancy_outline", numalign = "center"))
### Output
╒══════════════════════════╤══════════╤═══════╤═════════╤════════╤══════╤══════╤══════╤══════════╕
│ Club │ Played │ Won │ Drawn │ Lost │ GF │ GA │ GD │ Points │
╞══════════════════════════╪══════════╪═══════╪═════════╪════════╪══════╪══════╪══════╪══════════╡
│ Manchester City │ 38 │ 28 │ 5 │ 5 │ 94 │ 33 │ 61 │ 89 │
│ Arsenal │ 38 │ 26 │ 6 │ 6 │ 88 │ 43 │ 45 │ 84 │
│ Manchester United │ 38 │ 23 │ 6 │ 9 │ 58 │ 43 │ 15 │ 75 │
│ Newcastle United │ 38 │ 19 │ 14 │ 5 │ 68 │ 33 │ 35 │ 71 │
│ Liverpool │ 38 │ 19 │ 10 │ 9 │ 75 │ 47 │ 28 │ 67 │
│ Brighton And Hove Albion │ 38 │ 18 │ 8 │ 12 │ 72 │ 53 │ 19 │ 62 │
│ Aston Villa │ 38 │ 18 │ 7 │ 13 │ 51 │ 46 │ 5 │ 61 │
│ Tottenham Hotspur │ 38 │ 18 │ 6 │ 14 │ 70 │ 63 │ 7 │ 60 │
│ Brentford │ 38 │ 15 │ 14 │ 9 │ 58 │ 46 │ 12 │ 59 │
│ Fulham │ 38 │ 15 │ 7 │ 16 │ 55 │ 53 │ 2 │ 52 │
╘══════════════════════════╧══════════╧═══════╧═════════╧════════╧══════╧══════╧══════╧══════════╛
Tabulate also supports various table formats such as “plain”, “simple”, “pipe”, and more. You can customize the appearance of the table as per your preference.
Tabulate can create professional-looking tables easily compared to the standard method. You can apply it in various Python projects, from data analysis to reporting. Hopefully, this article has helped you understand how to use Tabulate to improve your data display.
Conclusion
We’ve explored using the Tabulate in Python to create neat and professional table data displays. Tabulates are helpful in various contexts, including data analysis, reporting, and documentation. With the help of this library, you can create more readable reports and better present the results of data analysis.
Moreover, learning and using the Tabulate is a good step in understanding how to work with Python libraries that serve to manipulate and manage data more efficiently. So, start your exploration with Tabulate, and you will find that it’s a handy tool in your programming journey.