Python:Pandas .last()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 31, 2025

The .last() method is an aggregation function used on a GroupBy object in Pandas. It returns the last non-null value in each section of the grouped data. This method is particularly useful for time-series data or sequentially ordered data where the final observation within a specific period or category is the most relevant.

  • 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

The method is called directly on a GroupBy object.

grouped_data.last(numeric_only=False, min_count=-1)

Parameters:

  • numeric_only (bool): If True, only attempts to aggregate numeric columns.
  • min_count (int): The required number of valid (non-NA) values to include the result. If fewer than min_count non-NA values are present, the result will be NaN.

Return value:

Returns a Series or DataFrame containing the last non-null value for each group.

Example

The following example demonstrates how to use .last() to find the final status of a task for each project:

import pandas as pd
# Create a DataFrame representing chronological task status updates
data = {
'project_id': ['A', 'B', 'A', 'C', 'B', 'C', 'A'],
'status_date': pd.to_datetime(['2025-01-01', '2025-01-05', '2025-01-10', '2025-01-15', '2025-01-20', '2025-01-25', '2025-01-30']),
'task_status': ['Start', 'Pending', 'In Progress', 'Start', 'Completed', 'In Progress', 'Completed']
}
df = pd.DataFrame(data).set_index('status_date').sort_index()
print("Original Data:\n", df)
# Group by project_id and find the last status (chronologically) for each project
last_status = df.groupby('project_id').last()
print("\nLast Status by Project:\n", last_status)

The above code returns the following output:

Original Data:
project_id task_status
status_date
2025-01-01 A Start
2025-01-05 B Pending
2025-01-10 A In Progress
2025-01-15 C Start
2025-01-20 B Completed
2025-01-25 C In Progress
2025-01-30 A Completed
Last Status by Project:
task_status
project_id
A Completed
B Completed
C In Progress

Codebyte Example

This codebyte example groups data by category and returns the last recorded value from each group:

Code
Output

All contributors

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