熊猫数据框获取每个组的第一行


137

我有DataFrame下面的熊猫。

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"]})

我想通过[“ id”,“ value”]对此分组,并获得每个分组的第一行。

        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

预期结果

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

我尝试了以下操作,仅给出的第一行DataFrame。任何有关此的帮助表示赞赏。

In [25]: for index, row in df.iterrows():
   ....:     df2 = pd.DataFrame(df.groupby(['id','value']).reset_index().ix[0])

2
我意识到这个问题已经很老了,但是我建议接受@vital_dml的回答,因为first()关于nans的行为非常令人惊讶,我想大多数人都不会想到。
user545424 '19

Answers:


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

如果需要id作为列:

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

要获取n条第一条记录,可以使用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

1
非常感谢!运作良好:)无法以相同的方式获得第二行吧?您能也解释一下吗?
Nilani Algiriyage

g = df.groupby(['session'])g.agg(lambda x:x.iloc [0])这也有效,不知道要获取第二个值吗?:(
Nilani Algiriyage

假设您要从顶部开始计数以获得行号top_n,然后dx = df.groupby('id')。head(top_n).reset_index(drop = True)并假设您要从底部开始计数以获得行号bottom_n,然后dx = df.groupby('id')。tail(bottom_n).reset_index(drop = True)
Quetzalcoatl

3
如果需要最后n行,请使用tail(n)(默认值为n = 5)(参考)。请勿混淆last(),我犯了那个错误。
rocarvaj

groupby('id',as_index=False)也保持id为专栏
Richard DiSalvo

50

这将为您提供每组的第二行(零索引,nth(0)与first()相同):

df.groupby('id').nth(1) 

文档:http : //pandas.pydata.org/pandas-docs/stable/groupby.html#taking-the-nth-row-of-each-group


8
如果您想使用倍数,例如前三个,请使用nth((0,1,2))或序列nth(range(3))
RonanPaixão16年

@RonanPaixão:不知何故,当我给范围时,它会引发一个错误:TypeError: n needs to be an int or a list/set/tuple of ints
和平的

@Peaceful:您正在使用Python 3吗?如果是这样,range(3)除非您输入,否则不会返回列表list(range(3))

41

我建议使用.nth(0)而不是.first()如果您需要获得第一行。

它们之间的区别在于它们处理NaN的方式,因此.nth(0)无论该行中的值是什么,都将返回组的第一行,而.first()最终将返回每列中的第一个not NaN值。

例如,如果您的数据集是:

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

>>> df.groupby('id').nth(0)
    value
id        
1    first
2    NaN
3    first
4    first

>>> df.groupby('id').first()
    value
id        
1    first
2    second
3    first
4    first

1
好点子。除了索引以外,其他的.head(1)行为也看起来像.nth(0)
理查德·迪萨沃

1
另一个区别是nth(0)将保留原始索引(如果as_index = False),而first()不会保留。对我而言,这是一个很大的区别,因为我需要索引本身。
Oleg O

7

也许这就是你想要的

import pandas as pd
idx = pd.MultiIndex.from_product([['state1','state2'],   ['county1','county2','county3','county4']])
df = pd.DataFrame({'pop': [12,15,65,42,78,67,55,31]}, index=idx)
                pop
state1 county1   12
       county2   15
       county3   65
       county4   42
state2 county1   78
       county2   67
       county3   55
       county4   31
df.groupby(level=0, group_keys=False).apply(lambda x: x.sort_values('pop', ascending=False)).groupby(level=0).head(3)

> Out[29]: 
                pop
state1 county3   65
       county4   42
       county2   15
state2 county1   78
       county2   67
       county3   55

7

如果只需要我们可以处理的每个组的第一行drop_duplicates,请注意函数default方法keep='first'

df.drop_duplicates('id')
Out[1027]: 
    id   value
0    1   first
3    2   first
5    3   first
9    4  second
11   5   first
12   6   first
15   7  fourth
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.