简体   繁体   English

DataFrame 问题(括号)

[英]DataFrame issue (parenthesis)

May I ask what's the difference between df.head() and df.head in python's syntax nature?请问 df.head() 和 df.head 在 python 的语法性质上有什么区别? Could I interpret as the former one is for calling a method and the later one is just trying to obtain the DataFrame's attribute, which is the head?我可以解释为前者用于调用方法,后者只是试图获取DataFrame的属性,即头部? I am so confused why sometimes there is a parenthesis at the end but sometimes not... Thank you so much.我很困惑为什么有时末尾有括号,但有时没有……非常感谢。

We use (parenthesis) for 'Methods' and for 'Attributes' (parenthesis) is not needed.我们将(括号)用于“方法”,而“属性”则不需要(括号)。 (parenthesis) is use to pass additional parameters into the method. (括号)用于将附加参数传递给方法。 for example df.head(5) gives you 5 items in the df at the top.例如 df.head(5) 在顶部的 df 中为您提供 5 个项目。 You can change that number to 2, 20... without the (parenthesis), you cannot pass that number.您可以将该数字更改为 2、20... 如果没有(括号),则无法传递该数字。

Whereas attributes are just describing the dataframe, for example df.shape,df.dtypes etc, you cannot use (parenthesis) with attributes虽然属性只是描述 dataframe,例如 df.shape、df.dtypes 等,但您不能将(括号)与属性一起使用

In this link dataframe you can find the attributes & methods available for a dataframe.在此链接dataframe 中,您可以找到可用于 dataframe 的属性和方法。

You understand it well, in general parenthesis call class method, and without you call an attribute.你理解的很好,一般括号里调用class方法,不用你调用一个属性。

In your exemple you don't have an error, because df.head is bound to NDFrame.head who is a method as well.在您的示例中,您没有错误,因为 df.head 绑定到 NDFrame.head 也是一种方法。 If df.head was only a method, calling it without parenthesis will raise an AttributeError.如果 df.head 只是一个方法,那么在没有括号的情况下调用它会引发 AttributeError。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM