python - 漂亮地打印 Pandas 数据框

标签 python pandas dataframe printing

如何将 pandas 数据框打印为漂亮的基于文本的表格,如下所示?

+------------+---------+-------------+
| column_one | col_two |   column_3  |
+------------+---------+-------------+
|          0 |  0.0001 | ABCD        |
|          1 |  1e-005 | ABCD        |
|          2 |  1e-006 | long string |
|          3 |  1e-007 | ABCD        |
+------------+---------+-------------+

最佳答案

我刚刚找到了一个很好的工具来满足这个需求,它叫做 tabulate .

它打印表格数据并与 DataFrame 一起使用。

from tabulate import tabulate
import pandas as pd

df = pd.DataFrame({'col_two' : [0.0001, 1e-005 , 1e-006, 1e-007],
                   'column_3' : ['ABCD', 'ABCD', 'long string', 'ABCD']})

print(tabulate(df, headers='keys', tablefmt='psql'))

+----+-----------+-------------+
|    |   col_two | column_3    |
|----+-----------+-------------|
|  0 |    0.0001 | ABCD        |
|  1 |    1e-05  | ABCD        |
|  2 |    1e-06  | long string |
|  3 |    1e-07  | ABCD        |
+----+-----------+-------------+

注意:

To suppress row indices for all types of data, pass showindex="never" or showindex=False.

关于python - 漂亮地打印 Pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18528533/

相关文章:

python - xlwings 循环遍历选定的单元格

python - 将行向左移动时删除特定单元格

optimization - 对 Pandas 系列进行迭代需要永远,但我想不出没有它的方法来解决这个问题。有没有更快的方法?

python - 获取 `pandas.DataFrame` 中列数总和最大的前 3 行?

python - Pandas 找到至少两组中存在的值

python - 使用 python 字典进行排序

python - [PyQt4]QPushButton焦点灯

python - 传递 Pandas 数据时,Scipy linregress 返回元组

python - 在多种条件下合并 Pandas 数据框(python/ Pandas )

python - Python matplotlib 如何处理二进制数据?