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

前端 未结 3 1982
半阙折子戏
半阙折子戏 2021-01-14 06:03

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 th

3条回答
  •  鱼传尺愫
    2021-01-14 06:57

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

    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
    

    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"

    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.

提交回复
热议问题