【问题标题】:How to stop Jupyter outputting truncated results when using pd.Series.value_counts()?使用 pd.Series.value_counts() 时如何停止 Jupyter 输出截断的结果?
【发布时间】:2017-10-10 03:01:15
【问题描述】:

我有一个DataFrame,我想使用pd.Series.value_counts() 显示某个Series 中某些值的频率。

问题是我只在输出中看到截断的结果。我正在 Jupyter Notebook 中编码。

我尝试了几种方法都不成功:

df = pd.DataFrame(...) # assume df is a DataFrame with many columns and rows

# 1st method
df.col1.value_counts()

# 2nd method
print(df.col1.value_counts())

# 3rd method
vals = df.col1.value_counts()
vals  # neither print(vals) doesn't work

# All output something like this
value1         100000
value2         10000
...
value1000      1

目前我用的是这个,但是比较麻烦:

print(df.col1.value_counts()[:50])
print(df.col1.value_counts()[50:100])
print(df.col1.value_counts()[100:150])
# etc.

另外,我已经阅读了this 相关的 Stack Overflow 问题,但没有发现它有帮助。

那么如何停止输出截断的结果呢?

【问题讨论】:

    标签: pandas jupyter-notebook


    【解决方案1】:

    如果要打印所有行:

    pd.options.display.max_rows = 1000
    print(vals)
    

    如果您只想打印所有行:

    with pd.option_context("display.max_rows", 1000):
        print(vals)
    

    相关文档here.

    【讨论】:

      【解决方案2】:

      我认为您需要 option_context 并设置为一些较大的数字,例如999.solution的优点是:

      option_context 上下文管理器 已通过顶级 API 公开,允许您执行具有给定选项值的代码。当您退出 with 块时,选项值会自动恢复。

      #temporaly display 999 rows
      with pd.option_context('display.max_rows', 999):
          print (df.col1.value_counts())
      

      【讨论】:

        猜你喜欢
        • 2011-12-11
        • 1970-01-01
        • 2020-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-07
        相关资源
        最近更新 更多