pandas dataframe groupby and get nth row

前端 未结 2 979
轻奢々
轻奢々 2020-12-06 03:03

I have a pandas DataFrame like following.

df = pd.DataFrame([[1.1, 1.1, 1.1, 2.6, 2.5, 3.4,2.6,2.6,3.4,3.4,2.6,1.1,1.1,3.3], list(\'AAABBBBABCBDDD\'), [1.1,          


        
2条回答
  •  旧巷少年郎
    2020-12-06 03:33

    I think the nth method is supposed to do just that:

    In [10]: g = df.groupby('ID')
    In [11]: g.nth(1).dropna()
    Out[11]: 
        col1 col2  col3     col4 col5
    ID                               
    1    1.1    D   4.7    x/y/z  200
    2    3.4    B   3.8    x/u/v  404
    3    1.1    A   2.5  x/y/z/n  404
    5    2.6    B   4.6      x/y  500
    

    In 0.13 another way to do this is to use cumcount:

    df[g.cumcount() == n - 1]
    

    ...which is significantly faster.

    In [21]: %timeit g.nth(1).dropna()
    100 loops, best of 3: 11.3 ms per loop
    
    In [22]: %timeit df[g.cumcount() == 1]
    1000 loops, best of 3: 286 µs per loop
    

提交回复
热议问题