Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (50.2k points)

In Jupyter Notebook or in the command prompt terminal, both df.head and df.head() can yield an output of the dataframe, with some lesser variations. What's the major difference between the two different expressions and what function does parenthesis play in Python in general? 

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

1 Answer

0 votes
by (108k points)
edited by

Please be informed that those are not just "minor variations". With the df.head, you have not actually extracted the top 5 records. Only the df.head() actually takes the head of the dataframe. The result is only having five number of 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

The df.head is just a function object for the head method of the dataframe df. The brackets are required to actually call the method. 

If you are a beginner and want to know more about Python, then do check out the below Python tutorial video that will help you in understanding the topic in a better way:

Browse Categories

...