在Pandas中使用groupby获取行数

3

我有一个数据集,其中有两列,col1col2。我想按照col1对数据进行分组显示。

为此,我编写了如下代码:

grouped = df[['col1','col2']].groupby(['col1'], as_index= False)

上述代码创建了一个groupby对象。
如何使用该对象按照col1分组显示数据?
2个回答

8
为了按组获取计数,您可以使用 dataframe.groupby('column').size()
示例:
In [10]:df = pd.DataFrame({'id' : [123,512,'zhub1', 12354.3, 129, 753, 295, 610],
                    'colour': ['black', 'white','white','white',
                            'black', 'black', 'white', 'white'],
                    'shape': ['round', 'triangular', 'triangular','triangular','square',
                                        'triangular','round','triangular']
                    },  columns= ['id','colour', 'shape'])

In [11]:df
Out[11]:
     id    colour   shape
0    123     black   round
1    512     white   triangular
2    zhub1   white   triangular
3    12354.3 white   triangular
4    129     black   square
5    753     black   triangular
6    295     white   round
7    610     white   triangular


In [12]:df.groupby('colour').size()
Out[12]:
        colour
        black     3
        white     5
        dtype: int64

In [13]:df.groupby('shape').size()
Out[13]:
        shape
        round         2
        square        1
        triangular    5
        dtype: int64

2
尝试使用groupby()返回的对象的groups属性和get_group()方法:
>>> import numpy as np
>>> import pandas as pd
>>> anarray=np.array([[0, 31], [1, 26], [0, 35], [1, 22], [0, 41]])
>>> df = pd.DataFrame(anarray, columns=['is_female', 'age'])
>>> by_gender=df[['is_female','age']].groupby(['is_female'])
>>> by_gender.groups # returns indexes of records
{0: [0, 2, 4], 1: [1, 3]}
>>> by_gender.get_group(0)['age'] # age of males
0    31
2    35
4    41
Name: age, dtype: int64

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接