df.head()和df.head有什么区别? [英] What's the difference between df.head() and df.head?
问题描述
在Jupyter Notebook或终端中,df.head和df.head()都可以返回数据帧的输出,但有一些细微差别.这两个不同表达式之间的根本区别是什么?括号通常在Python中扮演什么角色? 谢谢!
In Jupyter Notebook or terminal, both df.head and df.head() can return an output of the dataframe, with some minor differences. What's the fundamental difference between the two different expressions and what role does parenthesis play in Python in general? Thanks!
>>>df.head
<bound method NDFrame.head of Date Open High Low Close Volume
0 1-Jun-17 153.17 153.33 152.22 153.18 16404088
1 2-Jun-17 153.58 155.45 152.89 155.45 27770715
2 5-Jun-17 154.34 154.45 153.46 153.93 25331662
3 6-Jun-17 153.90 155.81 153.78 154.45 26624926
4 7-Jun-17 155.02 155.98 154.48 155.37 21069647
5 8-Jun-17 155.25 155.54 154.40 154.99 21250798
6 9-Jun-17 155.19 155.19 146.02 148.98 64882657
7 12-Jun-17 145.74 146.09 142.51 145.42 72307330
8 13-Jun-17 147.16 147.45 145.15 146.59 34165445
9 14-Jun-17 147.50 147.50 143.84 145.16 31531232
10 15-Jun-17 143.32 144.48 142.21 144.29 32165373
>>> df.head()
Date Open High Low Close Volume
0 1-Jun-17 153.17 153.33 152.22 153.18 16404088
1 2-Jun-17 153.58 155.45 152.89 155.45 27770715
2 5-Jun-17 154.34 154.45 153.46 153.93 25331662
3 6-Jun-17 153.90 155.81 153.78 154.45 26624926
4 7-Jun-17 155.02 155.98 154.48 155.37 21069647
推荐答案
这些不只是微小差异".实际上,df.head
根本使您无法接受.
Those aren't just "minor differences". You didn't actually take the head at all with df.head
.
df.head()
实际上是数据帧的开头.您可以看到输出只有5行:
df.head()
actually takes the head of the dataframe. You can see that the output only has 5 rows:
>>> df.head()
Date Open High Low Close Volume
0 1-Jun-17 153.17 153.33 152.22 153.18 16404088
1 2-Jun-17 153.58 155.45 152.89 155.45 27770715
2 5-Jun-17 154.34 154.45 153.46 153.93 25331662
3 6-Jun-17 153.90 155.81 153.78 154.45 26624926
4 7-Jun-17 155.02 155.98 154.48 155.37 21069647
相反,df.head
只是数据帧df
的head
方法的方法对象.需要括号才能实际调用该方法.方法对象的repr
基本上是
In contrast, df.head
is just a method object for the head
method of the dataframe df
. The parentheses are needed to actually call the method. The method object's repr
is basically
f"<bound method {classname}.{methodname} of {object!r}"
,在适当的位置替换了对象的类名称,方法名称和repr
.实际上,看起来像数据帧的输出部分是原始df
的repr
.它有10行而不是5行,因为它是整个原始数据帧,而不是头部.
with the class name, method name, and repr
of the object substituted in the appropriate places. The part of the output that looks like a dataframe is, in fact, the repr
of the original df
. It has 10 rows instead of 5 because it's the whole original dataframe, not the head.
这篇关于df.head()和df.head有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!