[Python]tabulate can create tables so elegantly

It would be very helpful for data analysis if we could quickly organize our unordered data into a more readable format. Python provides the ability to easily convert certain tabular data types into well-formed plain text tables, which is the tabulate library. If you like this article, remember to collect, like, and follow.

Without further ado, let's get right to it.

Preparation

2.1. Install tabulate library

It is very easy to install the tabulate library, you can use pip to install it, the code is as follows:

pip install tabulate

2.2. Import tabulate function

Then we need to import the tabulte function we need, as follows:

from tabulate import tabulate

After the preparations are done, let's give a chestnut next.

Use list to generate table

Let's assume we have the following data:

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

Then we can use the tabulate function to organize the above data into a more readable tabular form, "The code is as follows:"

print(tabulate(table))

The result is as follows:

Since the first list in the above list contains the name of each column, we can use the following parameters to display the column names individually, the code is as follows:

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

The result is as follows:

The tabulate function also provides a tablefmt parameter, which allows us to further improve the appearance of the table, the code is as follows:

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

The result is as follows:

Compared to grid, I prefer to use the fancy_grid parameter for tablefmt, which is expressed as follows:

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

The result is as follows:

Use dict to generate table

Of course, we can also use dictionaries to generate corresponding tables in Python.

"code show as below:"

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

In the case of dictionaries, the keys are usually the column headers and the values ​​will be the element values ​​of those columns. We usually specify that the keys are the headers of the table by passing "keys" as an argument to the headers parameter:

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

The result is as follows:

Of course, at this time we can also use the tablefmt parameter to improve the appearance of the table, the code is as follows:

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

The result is as follows:

Add index column

Further, we can also use the showindex parameter to add index columns to the table,

code show as below:

We can add custom indices by passing 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 an argument:

Missing value handling

If we remove 'Jennifer' from the dictionary, our table will now contain a blank cell with the following code:

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

The result is as follows:

有时候,我们觉得缺失值用空白格表示不太美观,此时我们可以设置默认值来显示,代码如下:

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

结果如下:

总结

本文介绍了在python中使用tabulate库来创建表格,并针对输出形式进行不断改进来美化输出效果,并给出了代码示例。

Guess you like

Origin blog.csdn.net/liuyukuan/article/details/128754899