head() method is used to return the first n (default 5) lines of a data frame or series.
n: integer value, number of rows to be returned
Return type: Dataframe with top n rows
To load the dataset used in the following example, press here.
In the following examples, the data frame used contains data for some NBA players. An image of the data frame before any operations is attached below.
Example # 1:
In this example, the top 5 rows of the data frame are returned and stored in a new variable. Parameters are not passed to the .head () method, since the default is 5.
As shown in the output image, you can see that the index of the rows returned is in the range 0 to 4. Therefore, the first 5 were returned lines.
Example # 2: Calling Series with the n () parameter
This example calls the .head () method on a series with user input of the n parameter to return the first 9 lines of the series.
As shown in the output image, the first 9 rows were returned in the range from 0 to about the 8th position of the index.