在默认情况下,我怎样才能查看一个系列或DataFrame中的所有行?

问题描述 投票:0回答:1

默认情况下,每当我查看一个系列或数据框架时,它只给我前五行和后五行的预览。我如何查看所有的行?有什么方法吗?

比如说

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个回答
0
投票

你可以像这样改变Jupyter的查看选项。

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

0
投票

一个替代方案 pd.set_option(). 创建一个自定义循环。在数据框架中以60组或任何你的最大打印行数循环。这种方法不排除每次迭代打印60行的列头,但它是一个有趣的 "替代 "代码,并证明似乎是一个可行的解决方案,打印大量的行> 100,000左右。我创建了一个随机的浮动数据框架,长度为10万行,运行时间为<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但随后,它变得更加技术化,你可能不得不去命令行和混乱的配置设置Jupyter笔记本的IOPub数据率超标(查看图像时)

我的解决方案可以让你打印所有的行,而不需要搞乱的 pd.options 或者必须在配置文件中手动编辑这些限制。当然,这同样取决于你想在终端中打印多少行。


-1
投票

这在下面的链接中会有解释。

https:/thispointer.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)
© www.soinside.com 2019 - 2024. All rights reserved.