Python - Compute first of group values in a Pandas DataFrame

PythonServer Side ProgrammingProgramming

To compute first of group values, use the groupby.first() method. At first, import the required library with an alias −

import pandas as pd;

Create a DataFrame with 3 columns −

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

Now, group DataFrame by a column −

groupDF = dataFrame.groupby("Car")

Compute first of group values and resetting index −

res = groupDF.first()
res = res.reset_index()

Example

Following is the complete code −

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("DataFrame ...\n",dataFrame)

# grouping DataFrame by column Car
groupDF = dataFrame.groupby("Car")

res = groupDF.first()
res = res.reset_index()

print("\nFirst of group values = \n",res)

Output

This will produce the following output. The first occurrence of repeated values are displayed i.e. first of group values −

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

First of group values =
     Car      Place   Units
0    BMW      Delhi     100
1  Lexus  Bangalore     150
2  Tesla     Punjab      80
raja
Updated on 29-Sep-2021 07:58:31

Advertisements