默认情况下,如何查看 Series and/or DataFrame 中的所有行?

By default, how can I view all the rows in a Series and/or DataFrame?

默认情况下,每当我查看 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

下面link对此进行了解释。

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

link 的摘录提供了这 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)

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

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

pd.set_option() 的替代方法。创建自定义循环。以 60 组为一组循环遍历数据框或任何用于打印的最大行数。这种方法不排除打印 60 行的每次迭代的列 headers,但编码很有趣 "alternative",结果证明它似乎是打印大于 100,000 行左右的大量行的可行解决方案.我创建了一个长度为 100,000 行的随机浮点数 据框,花费了不到 1 秒的时间 运行.

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

我的方法的好处取决于您要显示多少行。我刚刚在 100,000 行上使用 pd.set_options 方法尝试了最大行数,当仅调用 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 或不必在配置文件中手动编辑这些限制。当然,这再次取决于您要在终端中打印多少行。