How to get the first row of each group in pandas group-by?

In order to get the first row of each group in pandas group-by, the groupby() function can be used to group the data and the head() function can be used to select the first row of each group. The groupby() function will create a groupby object, and the head() function will select the first row of each group, making it quick and easy to access the first row of each group.


You can use the following basic syntax to get the first row of each group in a pandas DataFrame:

df.groupby('column_name').nth(0)

The following example shows how to use this syntax in practice.

Example: Get First Row of Each Group in Pandas

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'B', 'B', 'B', 'C', 'C', 'C'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 29],
                   'assists': [5, 19, 14, 8, 9, 12, 13, 8]})

#view DataFrame
df

	team	points	assists
0	A	18	5
1	A	22	19
2	B	19	14
3	B	14	8
4	B	14	9
5	C	11	12
6	C	20	13
7	C	29	8

We can use the following code to get the first row for each team:

#get first row for each team
df.groupby('team').nth(0)

	points	assists
team		
A	18	5
B	19	14
C	11	12

We can also specify as_index=False to keep the original index values:

#get first row for each team, keep original index values
df.groupby('team', as_index=False).nth(0)

        team	points	assists
0	A	18	5
2	B	19	14
5	C	11	12

Also note that you can pass a list of values to the nth() function if you’d like to get the first n rows for each group.

For example, the following code shows how to get the first two rows for each group:

#get first two rows for each team, keep original index values
df.groupby('team', as_index=False).nth((0, 1))

        team	points	assists
0	A	18	5
1	A	22	19
2	B	19	14
3	B	14	8
5	C	11	12
6	C	20	13

Note: You can find the complete documentation for the nth() function .

x