Skip to content
Advertisement

Pandas dataframe get first row of each group

I have a pandas DataFrame like following:

JavaScript

I want to group this by ["id","value"] and get the first row of each group:

JavaScript

Expected outcome:

JavaScript

I tried following, which only gives the first row of the DataFrame. Any help regarding this is appreciated.

JavaScript

Advertisement

Answer

JavaScript

If you need id as column:

JavaScript

To get n first records, you can use head():

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement