You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VS Code终端查看Python代码的完整输出?

解决VS Code终端中Pandas输出被截断的问题

你的问题是Pandas在终端输出DataFrame时自动截断了中间行,下面是几个直接有效的解决办法:

方法1:修改Pandas全局显示配置

在脚本开头添加Pandas的显示设置,强制显示所有行和列,这样所有内容都会完整输出:

import pandas as pd
# 设置显示所有行
pd.set_option('display.max_rows', None)
# 如果列也有截断问题,再加这行
pd.set_option('display.max_columns', None)

把这段代码加在你的import语句后面,整个脚本运行时所有DataFrame都会完整显示。

方法2:临时转换为字符串输出

如果不想全局修改设置,只针对当前DataFrame输出完整内容,可以用to_string()方法代替直接打印:
修改你的main函数里的打印语句:

print(descriptive_statistics.frequency_table().to_string())

这个方法会把DataFrame转换成完整的字符串,不会自动省略中间行。

方法3:调整VS Code终端缓冲区

如果终端本身的缓冲区大小不够导致内容被截断,可以调整VS Code的终端设置:

  1. 打开VS Code,按Ctrl+,(Windows/Linux)或Cmd+,(Mac)打开设置
  2. 在搜索框输入terminal.integrated.scrollback
  3. 把Terminal > Integrated: Scrollback的值调大(比如设为10000或更高),这样终端能保存更多历史输出内容

修改后的完整代码示例

import random
import numpy as np
import pandas as pd

# 添加Pandas显示设置
pd.set_option('display.max_rows', None)

class DescriptiveStatistics:
    def __init__(self, data):
        self.data = data
    
    def frequency_table(self):
        possible_values = sorted(set(self.data))
        counters = {x: 0 for x in possible_values}
        for x in counters:
            counters[x] = self.data.count(x)
        cumulative_relative_frequency = 0.0
        table_lines = []
        for k, v in counters.items():
            cumulative_relative_frequency += v/len(self.data)
            table_lines.append([k, v, v/len(self.data) ,cumulative_relative_frequency])
        return pd.DataFrame(
            table_lines,
            columns=[
                "Values",
                "Absolute Frequency",
                "Relative Frequemcy",
                "Cumulative Relative Frequency"
            ])

def main():
    print(f"===== Frequency Table =====")
    data = [random.randint(1,999) for _ in range(100)]
    descriptive_statistics = DescriptiveStatistics(data)
    # 或者用to_string():print(descriptive_statistics.frequency_table().to_string())
    print(descriptive_statistics.frequency_table())

if __name__ == "__main__":
    main()

内容的提问来源于stack exchange,提问作者Buraxta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 21:17:48