How to Get the First Row of Each Group in a Pandas DataFrame?

Susan Sarandon
Release: 2024-11-10 15:06:02
Original
368 people have browsed it

How to Get the First Row of Each Group in a Pandas DataFrame?

Getting the First Row of Each Group in a Pandas DataFrame

In pandas, groupby operations allow for efficient data aggregation and manipulation across different categories. However, retrieving specific rows within each group can be a challenge. This article will demonstrate how to retrieve the first row of each group when grouping a pandas DataFrame.

Problem:

We have a DataFrame with two columns, "id" and "value." We want to group the DataFrame by "id," "value," and get the first row of each group.

Expected Outcome:

id value
1 first
2 first
3 first
4 second
5 first
6 first
7 fourth

Solution:

To retrieve the first row of each group, we can use the .first() method. By passing "id" as the group key, .first() selects the first non-null element for each unique "id" group.

df.groupby('id').first()
Copy after login

This will produce the desired output, with the first row of each "id" group displayed.

Getting Identifier as Column:

If we need the identifier as a column, we can use .reset_index().

df.groupby('id').first().reset_index()
Copy after login

This yields:

id value
1 first
2 first
3 first
4 second
5 first
6 first
7 fourth

Retrieving Multiple Rows:

To retrieve the first n rows of each group, we can use .head().

df.groupby('id').head(2).reset_index(drop=True)
Copy after login

This allows us to retrieve specified number of rows from the beginning of each group.

The above is the detailed content of How to Get the First Row of Each Group in a Pandas DataFrame?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template