pandas df.head() 和 df.head 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53999279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:14:41  来源:igfitidea点击:

What's the difference between df.head() and df.head?

pythonpython-3.xpandasjupyter-notebook

提问by Jason Hu

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!

在 Jupyter Notebook 或终端中, df.head 和 df.head() 都可以返回数据帧的输出,但有一些细微的差别。这两种不同的表达式之间的根本区别是什么?括号在 Python 中通常扮演什么角色?谢谢!

>>>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

回答by user2357112 supports Monica

Those aren't just "minor differences". You didn't actually take the head at all with df.head.

这些不仅仅是“细微差别”。你实际上根本没有用df.head.

df.head()actually takes the head of the dataframe. You can see that the output only has 5 rows:

df.head()实际上是数据帧的头部。可以看到输出只有 5 行:

>>> 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

In contrast, df.headis just a method object for the headmethod of the dataframe df. The parentheses are needed to actually call the method. The method object's repris basically

相比之下,df.head只是headdataframe的方法的一个方法对象df。实际调用方法需要括号。方法对象的repr基本上是

f"<bound method {classname}.{methodname} of {object!r}"

with the class name, method name, and reprof the object substituted in the appropriate places. The part of the output that looks like a dataframe is, in fact, the reprof the original df. It has 10 rows instead of 5 because it's the whole original dataframe, not the head.

将类名、方法名和repr对象名替换在适当的位置。看起来像数据帧的输出部分实际上repr是原始df. 它有 10 行而不是 5 行,因为它是整个原始数据帧,而不是头部。

回答by YOBEN_S

headreturn a methodhead()return the top 5(default) row in your dataframe

head返回methodhead()数据框中的前 5(默认)行

type(df.head)
<class 'method'>
type(df.head())
<class 'pandas.core.frame.DataFrame'>

回答by Y0da

Parentheses are used to call a function. Let's take a small example with appendjust using it without parenthesis on a list will do nothing as it will just return the function itself but using the parenthesis call the function:

括号用于调用函数。让我们举一个小例子,append在列表上不带括号使用它不会做任何事情,因为它只会返回函数本身,但使用括号调用函数:

a = [1]
a.append
print(a)
[1]

a.append(2)
print(a)
[1, 2]

append = a.append
append(3)
print(a)
[1, 2, 3]

What you see when just using headis similar. They just added some code to actually call the function with default value.

刚使用时看到的内容head是类似的。他们只是添加了一些代码来实际调用具有默认值的函数。