Pandas Dataframe/Series.tail() method - Python
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. By default it returns the last 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 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 last 5 rows of the DataFrame are returned and stored in a new variable. Here, we will be using an NBA dataset which you can download from here.
import pandas as pd
data = pd.read_csv("/content/nba.csv")
print("NBA Dataset (Last 5 Rows):")
print(data.tail())
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:
- n: Number of rows to retrieve from the bottom of the DataFrame or Series.
Return: It returns the last n rows of the DataFrame or Series as a new DataFrame or Series.
Example of tail() method
Let’s see other examples for a better understanding.
1. Using tail() with a Custom Number of Rows
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.
import pandas as pd
data = pd.read_csv("/content/nba.csv")
series = data["Name"]
bottom = series.tail(n = 7)
print("NBA Dataset (Last 7 Rows):")
print(bottom)
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.
2. Using tail() on a Series
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.
import pandas as pd
data = pd.read_csv("/content/nba.csv")
salary = data['Salary']
print("Last 5 Salaries:")
print(salary.tail())
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.
3. Describing Specific Columns with tail()
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.
import pandas as pd
data = pd.read_csv("/content/nba.csv")
salary_name = data[['Name', 'Salary']].tail()
print("Last 5 Rows of Name and Salary Columns:")
print(salary_name)
Output:

By selecting just the "Name" and "Salary" columns we retrieve the last 5 rows of data for these two specific columns.
4. Using tail() After Sorting a DataFrame
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.
import pandas as pd
data = pd.read_csv("/content/nba.csv")
sorted_data = data.sort_values(by='Age', ascending=False)
bottom_sorted = sorted_data.tail()
print("Last 5 Rows After Sorting by Age:")
print(bottom_sorted)
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.