Home  >  Article  >  Backend Development  >  How to Get the First Row of Each Group in a Pandas DataFrame by Multiple Columns?

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

DDD
DDDOriginal
2024-11-10 04:54:02475browse

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

Get First Row of Each Group in a Pandas DataFrame by Multiple Columns

In a pandas DataFrame, it is often necessary to retrieve the first row of each group after grouping the DataFrame based on specific columns. This task can be accomplished efficiently using pandas' built-in methods.

To achieve this, one can use the .first() method to obtain the first (non-null) element for each group. The syntax for first() is as follows:

df.groupby('group_columns').first()

In your example, you want to group the DataFrame by both 'id' and 'value' columns and get the first row of each group. You can use the following code:

df.groupby(['id', 'value']).first()

This will produce the following result:

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

If you prefer to have 'id' as a column in the resulting DataFrame, you can reset the index using reset_index() as shown below:

df.groupby(['id', 'value']).first().reset_index()

The output of this operation will be:

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

Additionally, if you want to retrieve the first n rows within each group, you can use the .head() method instead of first(). For instance, to get the first two rows of each group, you can use:

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

This will return the following DataFrame:

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

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

Statement:
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