Pandas dataframe get first row of each group

Nilani Algiriyage

I have a pandas DataFrame like following.

df = pd.DataFrame({'id' : [1,1,1,2,2,3,3,3,3,4,4,5,6,6,6,7,7],
                'value'  : ["first","second","second","first",
                            "second","first","third","fourth",
                            "fifth","second","fifth","first",
                            "first","second","third","fourth","fifth"]})

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

        id   value
0        1   first
1        1  second
2        1  second
3        2   first
4        2  second
5        3   first
6        3   third
7        3  fourth
8        3   fifth
9        4  second
10       4   fifth
11       5   first
12       6   first
13       6  second
14       6   third
15       7  fourth
16       7   fifth

Expected outcome

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

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

In [25]: for index, row in df.iterrows():
   ....:     df2 = pd.DataFrame(df.groupby(['id','value']).reset_index().ix[0])
Roman Pekar
>>> df.groupby('id').first()
     value
id        
1    first
2    first
3    first
4   second
5    first
6    first
7   fourth

If you need id as column:

>>> df.groupby('id').first().reset_index()
   id   value
0   1   first
1   2   first
2   3   first
3   4  second
4   5   first
5   6   first
6   7  fourth

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

>>> df.groupby('id').head(2).reset_index(drop=True)
    id   value
0    1   first
1    1  second
2    2   first
3    2  second
4    3   first
5    3   third
6    4  second
7    4   fifth
8    5   first
9    6   first
10   6  second
11   7  fourth
12   7   fifth

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pandas dataframe get all rows between zero(0) of mask column and get first and last row of each group

From Dev

pandas dataframe compare first and last row from each group

From Dev

get first row for each group

From Dev

Pandas Dataframe: get average of first rows of each subgroup within a group

From Dev

How to divide pandas dataframe's value by its first row by each group?

From Dev

pandas: how do I select first row in each GROUP BY group?

From Dev

First row for each group

From Dev

How to get first row from each group from a table in sqlitedatabase?

From Dev

Find First Non-zero Value in Each Row of Pandas DataFrame

From Java

Select first row in each GROUP BY group?

From Dev

Select first row in each GROUP BY group

From Dev

Get the first cell from last row pandas dataframe

From Java

group by pandas dataframe and select latest in each group

From Dev

group by pandas dataframe and select latest in each group

From Dev

Group by and get the latest row from each group

From Java

How to select the first row of each group?

From Dev

Adding a rank to first row of each group

From Dev

KDB selecting first row from each group

From Dev

Laravel eloquent select first row of each group by

From Dev

Select first row in each contiguous run by group

From Dev

Select first row in each contiguous run by group

From Dev

pandas: Divide DataFrame last row by first row

From Dev

R delete last row in dataframe for each group

From Dev

Find first and last non-zero column in each row of a pandas dataframe

From Dev

Annotate each row with percent of total for group by, in pandas?

From Dev

How to fillna the last row of each group in Pandas?

From Dev

Python: Pandas - Delete the first row by group

From Dev

Start an iteration on first row of a group Pandas

From Dev

SQL Server Group By Query Select first row each group

Related Related

  1. 1

    Pandas dataframe get all rows between zero(0) of mask column and get first and last row of each group

  2. 2

    pandas dataframe compare first and last row from each group

  3. 3

    get first row for each group

  4. 4

    Pandas Dataframe: get average of first rows of each subgroup within a group

  5. 5

    How to divide pandas dataframe's value by its first row by each group?

  6. 6

    pandas: how do I select first row in each GROUP BY group?

  7. 7

    First row for each group

  8. 8

    How to get first row from each group from a table in sqlitedatabase?

  9. 9

    Find First Non-zero Value in Each Row of Pandas DataFrame

  10. 10

    Select first row in each GROUP BY group?

  11. 11

    Select first row in each GROUP BY group

  12. 12

    Get the first cell from last row pandas dataframe

  13. 13

    group by pandas dataframe and select latest in each group

  14. 14

    group by pandas dataframe and select latest in each group

  15. 15

    Group by and get the latest row from each group

  16. 16

    How to select the first row of each group?

  17. 17

    Adding a rank to first row of each group

  18. 18

    KDB selecting first row from each group

  19. 19

    Laravel eloquent select first row of each group by

  20. 20

    Select first row in each contiguous run by group

  21. 21

    Select first row in each contiguous run by group

  22. 22

    pandas: Divide DataFrame last row by first row

  23. 23

    R delete last row in dataframe for each group

  24. 24

    Find first and last non-zero column in each row of a pandas dataframe

  25. 25

    Annotate each row with percent of total for group by, in pandas?

  26. 26

    How to fillna the last row of each group in Pandas?

  27. 27

    Python: Pandas - Delete the first row by group

  28. 28

    Start an iteration on first row of a group Pandas

  29. 29

    SQL Server Group By Query Select first row each group

HotTag

Archive