Python - Compute first of group values in a Pandas DataFrame

To compute the first occurrence of group values in a Pandas DataFrame, use the groupby().first() method. This returns the first non-null value for each group, which is useful for data aggregation and analysis.

Syntax

DataFrame.groupby(by).first()

Creating a Sample DataFrame

Let's create a DataFrame with car data to demonstrate grouping ?

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

Computing First of Group Values

Group the DataFrame by the 'Car' column and get the first occurrence of each group ?

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 column and get first occurrence
result = dataFrame.groupby("Car").first().reset_index()

print("First of group values:")
print(result)
First of group values:
     Car      Place  Units
0    BMW      Delhi    100
1  Lexus  Bangalore    150
2  Tesla     Punjab     80

How It Works

The groupby("Car").first() method works as follows:

  • Groups the DataFrame by the 'Car' column
  • Selects the first row from each group
  • Returns a DataFrame with the first occurrence of each unique car
  • reset_index() converts the grouped column back to a regular column

Multiple Column Grouping

You can also group by multiple columns to get more specific first values ?

import pandas as pd

dataFrame = pd.DataFrame({
    "Car": ['BMW', 'BMW', 'BMW', 'Tesla', 'Tesla', 'Lexus'],
    "Type": ['Sedan', 'SUV', 'Sedan', 'Electric', 'Electric', 'SUV'],
    "Units": [100, 80, 120, 90, 95, 110]
})

print("Original DataFrame:")
print(dataFrame)
print()

# Group by both Car and Type
result = dataFrame.groupby(["Car", "Type"]).first().reset_index()
print("First of group values (Car + Type):")
print(result)
Original DataFrame:
     Car      Type  Units
0    BMW     Sedan    100
1    BMW       SUV     80
2    BMW     Sedan    120
3  Tesla  Electric     90
4  Tesla  Electric     95
5  Lexus       SUV    110

First of group values (Car + Type):
     Car      Type  Units
0    BMW     Sedan    100
1    BMW       SUV     80
2  Lexus       SUV    110
3  Tesla  Electric     90

Conclusion

The groupby().first() method efficiently retrieves the first occurrence of each group in a DataFrame. Use reset_index() to convert the grouped column back to a regular column for easier data manipulation.

Updated on: 2026-03-26T13:31:36+05:30

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements