![]() |
VOOZH | about |
The tail() method allows us to quickly preview the last few rows of a DataFrame or Series. This method is useful for data exploration as it helps us to inspect the bottom of the dataset without printing everything. It is commonly used to verify that data has been loaded correctly, check the last records and inspect the data towards the end of a dataset.
Let's see an example: Using tail() on a DataFrame
We will use the tail() method to retrieve the last few rows of a DataFrame. This provides a quick preview of the datasetβs structure and contents. By default, the method returns and stores the last 5 rows of the DataFrame in a new variable but we can customized this to return any number of rows.
Here, we will be using an NBA dataset which you can download from here.
Output:
The tail() method returns the last 5 rows of the dataset which provides a quick preview of the columns and their respective values at the bottom of the dataset.
Syntax:
DataFrame.tail(n=5)
Series.tail(n=5)
Parameter:
Return: It returns the last n rows of the DataFrame or Series as a new DataFrame or Series.
Letβs see other examples for a better understanding.
While the default number of rows returned by tail() 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 last 7 rows of the dataset which allows for a more focused preview of the bottom portion of the data.
The tail() method can also be used on a Pandas Series to retrieve the last few elements. This is useful when we're working with a single column of data and want to quickly inspect its contents.
Output:
The tail() method displays the last 5 salary entries from the "Salary" column which helps us focus on specific data when working with a single column.
We can also use the tail() method to preview specific columns of our dataset. This example focuses on previewing just the "Name" and "Salary" columns.
Output:
By selecting just the "Name" and "Salary" columns we retrieve the last 5 rows of data for these two specific columns.
In this example, we will sort the DataFrame by the "Age" column and then use tail() to preview the oldest players. This shows how tail() can be used in combination with sorting to inspect specific records.
Output:
After sorting by "Age" it helps us inspect the youngest players (the last records in the sorted DataFrame). By using .tail() we can efficiently check data integrity, inspect missing values and debug issues in our dataset.