【问题标题】:By default, how can I view all the rows in a Series and/or DataFrame?默认情况下,如何查看 Series 和/或 DataFrame 中的所有行?
【发布时间】:2020-04-23 00:38:02
【问题描述】:

默认情况下,每当我查看 Series 或 DataFrame 时,它​​只会给我前五行和最后五行作为预览。如何查看所有行?有没有办法呢?

例如,

df[df["First Name"].duplicated()]
    First Name  Gender  Start Date  Last Login Time Salary  Bonus % Senior Management   Team
327 Aaron   Male    1994-01-29  2020-04-22 18:48:00 58755   5.097   True    Marketing
440 Aaron   Male    1990-07-22  2020-04-22 14:53:00 52119   11.343  True    Client Services
937 Aaron   NaN 1986-01-22  2020-04-22 19:39:00 63126   18.424  False   Client Services
141 Adam    Male    1990-12-24  2020-04-22 20:57:00 110194  14.727  True    Product
302 Adam    Male    2007-07-05  2020-04-22 11:59:00 71276   5.027   True    Human Resources
... ... ... ... ... ... ... ... ...
902 NaN Male    2001-05-23  2020-04-22 19:52:00 103877  6.322   True    Distribution
925 NaN Female  2000-08-23  2020-04-22 16:19:00 95866   19.388  True    Sales
946 NaN Female  1985-09-15  2020-04-22 01:50:00 133472  16.941  True    Distribution
947 NaN Male    2012-07-30  2020-04-22 15:07:00 107351  5.329   True    Marketing
951 NaN Female  2010-09-14  2020-04-22 05:19:00 143638  9.662   True    NaN

【问题讨论】:

标签: python pandas dataframe series


【解决方案1】:

您可以像这样更改 Jupyter 的查看选项:

pd.set_option('display.max_rows', df.shape[0])

【讨论】:

    【解决方案2】:

    pd.set_option() 的替代方案。创建一个自定义循环。以 60 个为一组循环遍历数据框,或者您的最大行数用于打印。这种方法不排除打印 60 行的每次迭代的列标题,但它是一种有趣的编码“替代方案”,并且结果似乎是打印大量行 > 100,000 左右的可行解决方案。我创建了一个 100,000 行长的随机浮点数据帧,运行时间

    import numpy as np
    import pandas as pd
    import math
    nrows=100000
    df=pd.DataFrame(np.random.rand(nrows,4), columns=list('ABCD'))
    i=0
    for x in range(0,int(math.ceil(nrows/60))):
        print(df.iloc[i:i+60, :].tail(60))
        i+=60
    

    我的方法的好处取决于您要显示多少行。我刚刚使用 pd.set_options 方法在 100,000 行上尝试了最大行数,当我调用 df(而不是 print(df))时,我的页面变得无响应。那是因为,它创建了这么长的页面(没有滚动条),但是当您 print 时,您会得到一个滚动条,因此它的强度更低,并且更好地练习 IMO 来打印大量行。

    好的,那么打电话给df,我为什么不直接用pd.set_option('display.max_rows', None) 更改为最大限制并执行print(df)。那不行吗?

    这适用于 10,000 行,但我在执行 100,000 行时收到此错误。

    IOPub data rate exceeded.
    The notebook server will temporarily stop sending output
    to the client in order to avoid crashing it.
    To change this limit, set the config variable
    `--NotebookApp.iopub_data_rate_limit`.
    
    Current values:
    NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)
    NotebookApp.rate_limit_window=3.0 (secs)
    

    也许,你想调整NotebookApp.iopub_data_rate_limit,但它变得更加技术性,你可能不得不去命令行并弄乱配置设置 IOPub data rate exceeded in Jupyter notebook (when viewing image)

    我的解决方案允许您打印所有行,而不会弄乱pd.options 或不必在配置文件中手动编辑这些限制。当然,这同样取决于您要在终端中打印多少行。

    【讨论】:

      【解决方案3】:

      这在以下链接中进行了解释。

      https://thispointer.com/python-pandas-how-to-display-full-dataframe-i-e-print-all-rows-columns-without-truncation/

      链接的摘录提供了这 4 个选项。

      pd.set_option('display.max_rows', None)
      pd.set_option('display.max_columns', None)
      pd.set_option('display.width', None)
      pd.set_option('display.max_colwidth', -1)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-01
        • 2018-12-25
        • 2019-09-20
        • 1970-01-01
        相关资源
        最近更新 更多