如何在打印的表格中包含文本摘要?

3
假设我有两个数据框:
data1 = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'Height': [165, 182, 177]
}
df1 = pd.DataFrame(data1)

data2 = {
    'Summary': ["Alice is the youngest", "Bob is the tallest"]
}
df2 = pd.DataFrame(data2)

有没有办法我可以垂直连接这两个数据框,就像这样:
+---+---------+-----+----------+
|   |   Name  | Age |  Height  |
+---+---------+-----+----------+
| 0 |  Alice  |  25 |    165   |
| 1 |   Bob   |  30 |    182   |
| 2 | Charlie |  35 |    177   |
+---+---------+-----+----------+
|   |          Summary         |
+---+--------------------------+
| 0 | "Alice is the youngest"  |
| 1 |   "Bob is the tallest"   |
+---+--------------------------+


我迄今为止尝试的所有方法都将四列全部分组在顶部。

1
可能相关的帖子 https://stackoverflow.com/q/49533330/1328439 - undefined
1
还有一个相关的问题https://stackoverflow.com/q/57217927/1328439。在这里的结论是输出HTML,然后转换生成的HTML结构。 - undefined
2个回答

1
你可以使用tabulate并对输出进行后处理:
import tabulate as t

t.PRESERVE_WHITESPACE = True

width1 = 10
width2 = width1 * df1.shape[1] + df1.shape[1]*2

str1 = t.tabulate(df1.applymap(f'{{:^{width1}}}'.format), list(df1),
                  tablefmt='outline',
                  stralign='center', numalign='center')
str2 = t.tabulate(df2.applymap(f'{{:^{width2}}}'.format), df2.columns,
                  tablefmt='outline',
                  stralign='center', numalign='center')

print(str1 + '\n' + '\n'.join(str2.splitlines()[1:]))

输出:

+----+------------+------------+------------+
|    |    Name    |    Age     |   Height   |
+====+============+============+============+
| 0  |   Alice    |     25     |    165     |
| 1  |    Bob     |     30     |    182     |
| 2  |  Charlie   |     35     |    177     |
+----+------------+------------+------------+
|    |               Summary                |
+====+======================================+
| 0  |        Alice is the youngest         |
| 1  |          Bob is the tallest          |
+----+--------------------------------------+

或者根据df1的宽度动态调整df2。
import tabulate as t

t.PRESERVE_WHITESPACE = True

str1 = tabulate(df1, list(df1), tablefmt='outline',
                stralign='center', numalign='center')
H = str1.split('\n', 1)[0]
L = len(H)-H[1:].index('+')-5

str2 = tabulate(df2.applymap(f'{{:^{L}}}'.format),
                list(df2), tablefmt='outline',
                stralign='center', numalign='center')

print(str1 + '\n' + '\n'.join(str2.splitlines()[1:]))

输出:

+----+---------+-------+----------+
|    |  Name   |  Age  |  Height  |
+====+=========+=======+==========+
| 0  |  Alice  |  25   |   165    |
| 1  |   Bob   |  30   |   182    |
| 2  | Charlie |  35   |   177    |
+----+---------+-------+----------+
|    |          Summary           |
+====+============================+
| 0  |   Alice is the youngest    |
| 1  |     Bob is the tallest     |
+----+----------------------------+

我不确定.index('+')的目的是什么?我的两个数据框中的索引名称中都没有'+',单独调用这行代码会报错。你能进一步解释一下吗? - undefined
@vicefreak04 这是为了去除+----+---------+-------+----------+这一行,因为我们绘制了两个表格,会导致重复。此时与底层数据无关,我们只有一个表格的字符串表示。 - undefined

0
这在pandas中是不可能的。将数据保持在干净的DataFrame中,然后在需要显示的地方进行格式化。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接