alEx
alEx

Reputation: 310

how to get the first or last row while looping over grouped panda dataframe

I am looping over groups of a pandas dataframe:

for group_number, group in df.groupby( "group_number")

in this loop the rows are order by date and I want to access values in the first and in the last rows (start and end of records in the group). Unfortunately first() and last() don't work for the groups in this loop.

Can I do that with dataframes or do I have to loop over a list of list of tuples ?

Thanks for your help

Upvotes: 0

Views: 1323

Answers (2)

alEx
alEx

Reputation: 310

@Joshua Voskamp I didn't see this possibility :) Also it returns dataframe and if in my loop I want to access specific row/column values I have to work on it a bit more :

for group_number, group in df.groupby('group number'):
    print(group.take([0,-1], axis=1).iloc[0].values)

In my case it's simpler to use to access specific values :

for group_number, group in df.groupby('group number'):
    print(group.iloc[ 0].data, group.iloc[ -1].data,)

Maybe I should have use these fonction (first, last, nth, take, ...) and at the end merge/join the dataframes in one (not looping at all) !

Thanks :)

Upvotes: 0

Joshua Voskamp
Joshua Voskamp

Reputation: 2054

Get your first and last from your groupby, using take, and then operate on that:

for group_number, group in df.groupby("group number"):
    group.take([0,-1])

For example, using a filler df:

>>> df = pd.DataFrame({'group number':np.repeat(np.arange(1,4),4),\
           'data':list('abcd1234wxyz')})
>>> df
    group number data
0              1    a
1              1    b
2              1    c
3              1    d
4              2    1
5              2    2
6              2    3
7              2    4
8              3    w
9              3    x
10             3    y
11             3    z
>>> for group_number, group in df.groupby('group number'):
    print(group.take([0,-1]))

    
   group number data
0             1    a
3             1    d
   group number data
4             2    1
7             2    4
    group number data
8              3    w
11             3    z

Upvotes: 1

Related Questions