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 here.
Additional Resources
The following tutorials explain how to perform other common operations in pandas:
How to Get First Row of Pandas DataFrame
How to Drop First Row in Pandas DataFrame
How to Insert a Row Into a Pandas DataFrame