Python:Pandas .tail()

Anonymous contributor's avatar
Anonymous contributor
Published Jul 8, 2024
Contribute to Docs

In Pandas, .tail() is a method that returns the last n rows of a DataFrame. By default, it returns the last 5 rows, but the number of rows can be adjusted by passing an integer argument to the method.

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

df.tail(n=5)
  • df: The Pandas DataFrame on which the method is called.
  • n: The number of rows to return from the end of the DataFrame.

Example

The example below demonstrates the use of the .tail() method:

import pandas as pd
# Create a DataFrame
df = pd.DataFrame({'A': [1, 2, 3, 4, 5, 6], 'B': [7, 8, 9, 10, 11, 12]})
# Get the last 3 rows of the DataFrame
last_three_rows = df.tail(3)
# Print the last 3 rows
print(last_three_rows)

The output of the code above will be:

A B
3 4 10
4 5 11
5 6 12

Codebyte Example

The codebyte example below shows how the .tail() method works:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:Pandas on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours