![]() |
VOOZH | about |
The head() method structure and contents of our dataset without printing everything. By default it returns the first five rows but this can be customized to return any number of rows. It is commonly used to verify that data has been loaded correctly, check column names and inspect the initial records.
Lets see an example of using head() on a DataFrame
We will see how to use the head() method to retrieve the first few rows of the DataFrame(). This provides a quick preview of the datasetβs structure and contents.
Here we will be using a NBA dataset which you can download from here.
Output:
This is useful for verifying that the data is loaded correctly and for quickly understanding the structure of the dataset.
Syntax:
DataFrame.head(n=5)
Series.head(n=5)
Parameter:
Return: It returns the first n rows of the DataFrame or Series as a new DataFrame or Series.
Lets see other examples for its better understanding.
While the default number of rows returned by head() is 5 but we can customize this number to preview a specific subset of the data. This is useful when we want to see more or fewer rows.
Output:
By specifying 7, we retrieve the first seven rows of the dataset. This is helpful when we need to inspect a larger portion of the data than the default without printing the entire set.
The head() method can also be used on a Pandas Series to retrieve the first few elements. This is useful when we're working with a single column of data and want to quickly inspect its contents.
Output:
We retrieve the first 5 salary entries from the "Salary" column.
We can also use the head() method to preview specific columns of our dataset. This example focuses on previewing just the "Name" and "Salary" columns.
Output:
We preview the first 5 rows of just the "Name" and "Salary" columns, allowing us to focus on these specific features.
Here we will use head() to inspect the first few rows of a DataFrame after sorting it by a specific column. This is useful when we want to identify the top records based on a specific criterion like the highest salary or the youngest player.
Output:
After sorting the dataset by the "Age" column, we use head() to retrieve the first 5 rows which now correspond to the youngest players in the NBA dataset.
By mastering the head() method, we can easily navigate through our data which ensures a smooth start to our analysis and making it easier to spot key insights right from the beginning.