【问题标题】:Pretty print a pandas dataframe in VS Code在 VS Code 中漂亮地打印熊猫数据框
【发布时间】:2019-02-12 23:10:09
【问题描述】:

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

感谢您的帮助。


df 打印在 vs 代码中:

df 在 pycharm 中打印:

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 我想知道为什么这只能在“变量”中使用,而不是在“手表”中。有时使用“Watch”更方便,例如当局部变量太多时,您需要检索您想要的那个。但无论如何都有很大的改进
    • 终于可以看到我的DataFrame了!
    【解决方案2】:

    Tabulate 是一个出色的库,可以实现 pandas 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 |
    +----+--------+-------+---------------+-----------------+
    

    漂亮又香脆的印刷品,尽情享受吧!!!如果您喜欢我的回答,请添加 cmets!

    【讨论】:

    【解决方案3】:

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2013-09-02
        • 2017-12-10
        • 2021-06-22
        • 2018-05-10
        • 1970-01-01
        • 2017-12-19
        • 1970-01-01
        相关资源
        最近更新 更多