Python - Compute last of group values in a Pandas DataFrame

The groupby().last() method in Pandas returns the last row of each group when data is grouped by one or more columns. This is useful for getting the most recent entry for each category in your dataset.

Creating Sample Data

First, let's create a DataFrame with some sample car sales data ?

import pandas as pd

dataFrame = pd.DataFrame({
    "Car": ['BMW', 'Lexus', 'BMW', 'Tesla', 'Lexus', 'Tesla'],
    "Place": ['Delhi', 'Bangalore', 'Pune', 'Punjab', 'Chandigarh', 'Mumbai'],
    "Units": [100, 150, 50, 80, 110, 90]
})

print("Original DataFrame:")
print(dataFrame)
Original DataFrame:
     Car       Place  Units
0    BMW       Delhi    100
1  Lexus   Bangalore    150
2    BMW        Pune     50
3  Tesla      Punjab     80
4  Lexus  Chandigarh    110
5  Tesla      Mumbai     90

Using groupby().last() Method

Group the DataFrame by the "Car" column and get the last occurrence of each car ?

import pandas as pd

dataFrame = pd.DataFrame({
    "Car": ['BMW', 'Lexus', 'BMW', 'Tesla', 'Lexus', 'Tesla'],
    "Place": ['Delhi', 'Bangalore', 'Pune', 'Punjab', 'Chandigarh', 'Mumbai'],
    "Units": [100, 150, 50, 80, 110, 90]
})

# Group by Car and get last occurrence
groupDF = dataFrame.groupby("Car")
result = groupDF.last()

print("Last occurrence of each car:")
print(result)
Last occurrence of each car:
         Place  Units
Car                  
BMW       Pune     50
Lexus  Chandigarh    110
Tesla    Mumbai     90

Resetting Index

To convert the grouped result back to a regular DataFrame with "Car" as a column instead of index ?

import pandas as pd

dataFrame = pd.DataFrame({
    "Car": ['BMW', 'Lexus', 'BMW', 'Tesla', 'Lexus', 'Tesla'],
    "Place": ['Delhi', 'Bangalore', 'Pune', 'Punjab', 'Chandigarh', 'Mumbai'],
    "Units": [100, 150, 50, 80, 110, 90]
})

# Group by Car, get last occurrence, and reset index
result = dataFrame.groupby("Car").last().reset_index()

print("Last of group values with reset index:")
print(result)
Last of group values with reset index:
     Car       Place  Units
0    BMW        Pune     50
1  Lexus  Chandigarh    110
2  Tesla      Mumbai     90

How It Works

The groupby().last() method:

  • Groups rows based on the specified column(s)
  • Returns the last row from each group based on the original order
  • Excludes NaN values by default when determining the last value
  • Maintains the data types of the original columns

Conclusion

Use groupby().last() to get the final occurrence of each group in a Pandas DataFrame. This method is particularly useful for time-series data or when you need the most recent entry for each category.

Updated on: 2026-03-26T02:15:05+05:30

222 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements