在VS Code中漂亮地打印熊猫数据框

时间:2018-09-07 15:02:21

标签: python pandas debugging dataframe visual-studio-code

我想知道在调试时是否可以在VS Code中显示熊猫数据框(第一张图片),因为它在PyCharm中显示(第二张图片)?

感谢您的帮助。


df使用vs代码打印:

df print in vs code

df以pycharm打印:

df print in pycharm

4 个答案:

答案 0 :(得分:12)

从 python 扩展的 January 2021 release 开始,您现在可以在调试本机 python 程序时使用内置数据查看器查看 Pandas 数据帧。当程序在断点处停止时,右键单击变量列表中的数据框变量并选择“在数据查看器中查看值”

DataViewerWhenDebugging

答案 1 :(得分:1)

我还没有找到VS Code的类似功能。如果需要此功能,则可以考虑使用Spyder IDE。 Spyder IDE Homepage

答案 2 :(得分:1)

Tabulate是一个出色的库,可以实现对熊猫df的精美/精美打印:

信息-链接:[https://pypi.org/project/tabulate/]

请按照以下步骤进行打印: (注意:为便于说明,我将在python中创建简单的数据框)

1)安装列表

pip install --upgrade tabulate

此语句将始终安装列表库的最新版本。

2)导入语句

import pandas as pd
from tabulate import tabulate

3)创建简单的临时数据框

temp_data = {'Name': ['Sean', 'Ana', 'KK', 'Kelly', 'Amanda'], 
        'Age': [42, 52, 36, 24, 73], 
        'Maths_Score': [67, 43, 65, 78, 97],
        'English_Score': [78, 98, 45, 67, 64]}
df = pd.DataFrame(temp_data, columns = ['Name', 'Age', 'Maths_Score', 'English_Score'])

4)不以表格形式显示我们的数据框打印结果将是:

print(df)

    Name  Age  Maths_Score  English_Score
0    Sean   42           67             78
1     Ana   52           43             98
2      KK   36           65             45
3   Kelly   24           78             67
4  Amanda   73           97             64

5)使用表格后,您的漂亮图片将是:

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

+----+--------+-------+---------------+-----------------+
|    | Name   |   Age |   Maths_Score |   English_Score |
|----+--------+-------+---------------+-----------------|
|  0 | Sean   |    42 |            67 |              78 |
|  1 | Ana    |    52 |            43 |              98 |
|  2 | KK     |    36 |            65 |              45 |
|  3 | Kelly  |    24 |            78 |              67 |
|  4 | Amanda |    73 |            97 |              64 |
+----+--------+-------+---------------+-----------------+

好吃的脆皮印刷品,尽情享受!!!如果您喜欢我的答案,请添加评论!

答案 3 :(得分:0)