大 pandas 数据帧groupby并获得第n行 [英] pandas dataframe groupby and get nth row

查看:116
本文介绍了大 pandas 数据帧groupby并获得第n行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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],(AAABBBBABCBDDD),[1.1,1.7,2.5,2.6,3.3,3.8,4.0,4.2,4.3,4.5, (x / y / z',x / y',x / y / z / n','x / u','x','x / u / v ','x / y / z','x','x / u / v / b',' - ','x / y','x / y / z' / v / w'],['1','3','3','2','4','2','5','3','6','3' ','1','1','1'],['200','400','404','200','200','404','200' ','200','500','200','200','400']])T 

df.columns = ['col1','col2','col3' ,'col4','ID','col5']

并获得每组的第二排。后来我也需要得到第3和第4。只需解释一下,如何只得到每个组的第二行。



我尝试了以下第一和第二个。


$ b $ ($)

相反,我只需要获得第二行。由于ID 4和6没有第二行需要忽略它们。

  col1 col2 col3 col4 ID col5 
ID
1 0 1.1 A 1.1 x / y / z 1 200
11 1.1 D 4.7 x / y / z 1 200
2 3 2.6 B 2.6 x / u 2 200
5 3.4 B 3.8 x / u / v 2 404
3 1 1.1 A 1.7 x / y 3 400
2 1.1 A 2.5 x / y / z / n 3 404
4 4 2.5 B 3.3 x 4 200
5 6 2.6 B 4 x / y / z 5 200
10 2.6 B 4.6 x / y 5 500
6 8 3.4 B 4.3 x / u / v / b 6 500


解决方案

我认为第n种方法应该是这样做的:

 在[11]中:g.nth(1).dropna()
输出[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

在0.13的另一种方法是使用cumcount:

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

...这是显着

 在[21]中:%timeit g.nth 1).dropna()
100循环,最好3:11.3 ms每循环

在[22]中:%timeit df [g.cumcount()== 1]
1000循环,最佳3:286μs每循环


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, 1.7, 2.5, 2.6, 3.3, 3.8,4.0,4.2,4.3,4.5,4.6,4.7,4.7,4.8], ['x/y/z','x/y','x/y/z/n','x/u','x','x/u/v','x/y/z','x','x/u/v/b','-','x/y','x/y/z','x','x/u/v/w'],['1','3','3','2','4','2','5','3','6','3','5','1','1','1'],['200','400','404','200','200','404','200','404','500','200','500','200','200','400']]).T

df.columns = ['col1','col2','col3','col4','ID','col5']

I want group this by "ID" and get the 2nd row of each group. Later I will need to get 3rd and 4th also. Just explain me how to get only the 2nd row of each group.

I tried following which gives both first and second.

df.groupby('ID').head(2)

Instead I need to get only the second row. Since ID 4 and 6 has no second rows need to ignore them.

             col1 col2 col3     col4     ID    col5
ID                                           
1       0   1.1     A  1.1    x/y/z       1    200
        11  1.1     D  4.7    x/y/z       1    200
2       3   2.6     B  2.6      x/u       2    200
        5   3.4     B  3.8    x/u/v       2    404
3       1   1.1     A  1.7      x/y       3    400
        2   1.1     A  2.5  x/y/z/n       3    404
4       4   2.5     B  3.3        x       4    200
5       6   2.6     B    4    x/y/z       5    200
        10  2.6     B  4.6      x/y       5    500
6       8   3.4     B  4.3  x/u/v/b       6    500

解决方案

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

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

这篇关于大 pandas 数据帧groupby并获得第n行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆