列出元素与数据帧描述何时发生?

chr*_*sfs 4 python pandas

这可能很简单,但我无法弄清楚出了什么问题.我在列出DataFrame的元素时遇到问题.有时会列出DataFrame的元素,有时它只是对数据列的数量和类型的描述.我知道行数是一个因素,但即使我只有几行,我也只能得到描述.例如:如果我有一个名为'allpledges'的DataFrame,它会给我一个描述

In [5]:

allpledges

Out[5]:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 305384 entries, 0 to 305383
Data columns:
Pledge#       305384  non-null values
Source        305384  non-null values
Date          305384  non-null values
Break         305384  non-null values
Progcode      237002  non-null values
Run Code Online (Sandbox Code Playgroud)

这是可以理解的,因为要显示的行太多.但是当我试着看一些时,它仍然给了我同样的东西

In [13]:

allpledges[:5]

Out[13]:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 0 to 4
Data columns:
Pledge#       5  non-null values
Source        5  non-null values
Date          5  non-null values
Break         5  non-null values
Progcode      0  non-null values
Run Code Online (Sandbox Code Playgroud)

当我想要的是列出的前五行.我已经在教程中看到了这一点,但无法弄清楚我在这里做错了什么.

And*_*den 6

这些设置是在options(我怀疑你正在寻找max_rowsmax_columns,但在set_optionsdocstring中有很多选项)控制的:

In [11]: pd.options.display.max_columns
Out[11]: 20
Run Code Online (Sandbox Code Playgroud)

并使用set_option以下方法更改它

In [12]: pd.set_option('display.max_columns', 10)
Run Code Online (Sandbox Code Playgroud)

如果数据帧或者比这些设置的详细列或多行会缩写.

例如:

In [17]: df = pd.DataFrame(pd.np.arange(10).reshape(5,2))

In [18]: pd.set_option('display.max_rows', 4)

In [19]: df
Out[19]: 
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 0 to 4
Data columns:
0    5  non-null values
1    5  non-null values
dtypes: int64(2
Run Code Online (Sandbox Code Playgroud)