Pandas DataFrame.tail


Pandas DataFrame.tail

The DataFrame.tail method in pandas is used to return the last n rows of a DataFrame. This method is useful for inspecting the end of a dataset, especially when working with large DataFrames.


Syntax

The syntax for DataFrame.tail is:

DataFrame.tail(n=5)

Here, DataFrame refers to the pandas DataFrame from which the last n rows are returned.


Parameters

ParameterDescription
nAn integer specifying the number of rows to return. Defaults to 5. If n is greater than the number of rows in the DataFrame, the entire DataFrame is returned.

Returns

A pandas DataFrame containing the last n rows of the original DataFrame.


Examples

Returning the Last 5 Rows

By default, tail returns the last 5 rows of the DataFrame.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Priya', 'Kiran', 'Sita', 'Mohan'],
    'Age': [25, 30, 35, 40, 28, 32],
    'Salary': [70000, 80000, 90000, 60000, 75000, 82000]
}
df = pd.DataFrame(data)

# Return the last 5 rows
print("Last 5 Rows:")
print(df.tail())

Output

Last 5 Rows:
    Name  Age  Salary
1    Ram   30   80000
2   Priya   35   90000
3  Kiran   40   60000
4    Sita   28   75000
5   Mohan   32   82000

Returning the Last 3 Rows

Specify the number of rows to return by passing a value to the n parameter.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Priya', 'Kiran', 'Sita', 'Mohan'],
    'Age': [25, 30, 35, 40, 28, 32],
    'Salary': [70000, 80000, 90000, 60000, 75000, 82000]
}
df = pd.DataFrame(data)

# Return the last 3 rows
print("Last 3 Rows:")
print(df.tail(3))

Output

Last 3 Rows:
    Name  Age  Salary
3  Kiran   40   60000
4    Sita   28   75000
5   Mohan   32   82000

Returning the Entire DataFrame

If n is greater than the number of rows in the DataFrame, the entire DataFrame is returned.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Name': ['Arjun', 'Ram', 'Priya'],
    'Age': [25, 30, 35],
    'Salary': [70000, 80000, 90000]
}
df = pd.DataFrame(data)

# Return more rows than available
print("Returning more rows than available:")
print(df.tail(10))

Output

Returning more rows than available:
    Name  Age  Salary
0  Arjun   25   70000
1    Ram   30   80000
2   Priya   35   90000

Using tail on an Empty DataFrame

Using tail on an empty DataFrame will return an empty DataFrame.

Python Program

import pandas as pd

# Create an empty DataFrame
df_empty = pd.DataFrame()

# Use tail on the empty DataFrame
print("Using tail on an empty DataFrame:")
print(df_empty.tail())

Output

Using tail on an empty DataFrame:
Empty DataFrame
Columns: []
Index: []

Summary

In this tutorial, we explored the DataFrame.tail method in pandas. Key takeaways include:

  • Using tail to inspect the last n rows of a DataFrame.
  • Specifying the number of rows with the n parameter.
  • Handling cases where n exceeds the number of rows or when the DataFrame is empty.

The DataFrame.tail method is a quick and easy way to inspect the end of a dataset during data analysis.


Python Libraries