pandas dataframe groupby and get nth row

前端 未结 2 983
轻奢々
轻奢々 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:30

    If you use apply on the groupby, the function you pass is called on each group, passed as a DataFrame. So you can do:

    df.groupby('ID').apply(lambda t: t.iloc[1])
    

    However, this will raise an error if the group doesn't have at least two rows. If you want to exclude groups with fewer than two rows, that could be trickier. I'm not aware of a way to exclude the result of apply only for certain groups. You could try filtering the group list first by removing small groups, or return a one-row nan-filled DataFrame and do dropna on the result.

提交回复
热议问题