• Python中表格插件Tabulate的用法
  • 发布于 2个月前
  • 538 热度
    0 评论

Tabulate是一个用Python编写的库,可以方便地创建交互式表格和可视化数据。它适用于从简单的数据集到复杂的多列数据,并能够将数据以各种格式输出,如HTML、Markdown、CSV等。Tabulate的表格可以包含异构的数据,例如字符串、数字、日期等。Tabulate由Jinja2的作者Armin Ronacher开发,使用Python的灵活性和可读性提供了一种简单的方法来创建表格。它支持自定义表格样式、排序、过滤和分页,以及在表格中添加公式和统计信息。

Tabulate的安装方法因操作系统和Python版本而异,可以在终端或命令提示符下使用pip install tabulate命令进行安装。在安装完成后,您可以在Python代码中导入tabulate库并使用其中的函数来创建和打印表格。

这个库可以在 Python 中打印出漂亮的表格,允许智能和可定制的列对齐、数字和文本格式、小数点对齐,也是一个数据分析过程中的好用工具。支持的数据类型包括 dataframe, list of lists or dictionaries, dictionary, NumPy array。

案例1:生成基础表格

from tabulate import tabulate
df_pretty_printed = df.iloc[:5, [1,2,4,6]]
print(tabulate(df_pretty_printed))
-  -----------  -----------------------  ------  -----
0  Jadzia       Domestic Short Hair      Female  Stray
1  Gonzo        German Shepherd Dog/Mix  Male    Stray
2  Maggie       Shep Mix/Siberian Husky  Female  Stray
3  Pretty Girl  Domestic Short Hair      Female  Stray
4  Pretty Girl  Domestic Short Hair      Female  Stray
-  -----------  -----------------------  ------  -----
我们还可以自定义表格头,使用参数 headers
print(tabulate(
               df_pretty_printed,
               headers='keys',
               tablefmt='fancy_grid',
               stralign='center'
               ))
│    │  animalname  │        breedname        │  sexname  │  returnedreason  │
╞════╪══════════════╪═════════════════════════╪═══════════╪══════════════════╡
│  0 │    Jadzia    │   Domestic Short Hair   │  Female   │      Stray       │
├────┼──────────────┼─────────────────────────┼───────────┼──────────────────┤
│  1 │    Gonzo     │ German Shepherd Dog/Mix │   Male    │      Stray       │
├────┼──────────────┼─────────────────────────┼───────────┼──────────────────┤
│  2 │    Maggie    │ Shep Mix/Siberian Husky │  Female   │      Stray       │
├────┼──────────────┼─────────────────────────┼───────────┼──────────────────┤
│  3 │ Pretty Girl  │   Domestic Short Hair   │  Female   │      Stray       │
├────┼──────────────┼─────────────────────────┼───────────┼──────────────────┤
│  4 │ Pretty Girl  │   Domestic Short Hair   │  Female   │      Stray       │
╘════╧══════════════╧═════════════════════════╧═══════════╧══════════════════╛

不过这个库打印出的表格数据在手机屏幕上会有一定的兼容性问题,只有在PC机上才能有最佳的显示效果。

案例2使用Tabulate来创建一个包含5行3列数据的表格,并将其输出为HTML格式

from tabulate import tabulate  
// 堆代码 duidaima.com
list_ = [['张三', '90班', '98'], ['张三', '90班', '98'], ['张三', '90班', '98'], ['张三', '90班', '98'], ['张三', '90班', '98']]  
print(tabulate(list_, headers=['姓名', '班级', '成绩'], tableclass='docutils table'))
html代码:

<table class="docutils table">  
<colgroup>  
<col style="width: 33.3333%;" />  
<col style="width: 33.3333%;" />  
<col style="width: 33.3334%;" />  
</colgroup>  
<thead>  
<tr class="row-header">  
<th class="col-header">姓名</th>  
<th class="col-header">班级</th>  
<th class="col-header">成绩</th>  
</tr>  
</thead>  
<tbody>  
<tr class="row-even">  
<td class="entry">张三</td>  
<td class="entry">90班</td>  
<td class="entry">98</td>  
</tr>  
<tr class="row-odd">  
<td class="entry">张三</td>  
<td class="entry">90班</td>  
<td class="entry">98</td>  
</tr>  
<tr class="row-even">  
<td class="entry">张三</td>  
<td class="entry">90班</td>  
<td class="entry">98</td>  
</tr>  
<tr class="row-odd">  
<td class="entry">张三</td>  
<td class="entry">90班</td>  
<td class="entry">98</td>  
</tr>  
<tr class="row-even">  
<td class="entry">张三</td>  
<td class="entry">90班</td>  
<td class="entry">98</td>  
</tr>  
</tbody>  
</table>



用户评论