【问题标题】:Getting count of rows using groupby in Pandas在 Pandas 中使用 groupby 获取行数
【发布时间】:2014-04-11 14:44:50
【问题描述】:

我的数据集中有两列,col1col2。我想显示按col1 分组的数据。

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

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

上面的代码创建了 groupby 对象。

如何使用对象显示按col1分组的数据?

【问题讨论】:

    标签: python-3.x numpy pandas


    【解决方案1】:

    要通过group 获取计数,您可以使用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
      

      【讨论】:

        猜你喜欢
        • 2021-07-22
        • 1970-01-01
        • 2019-06-06
        • 1970-01-01
        • 1970-01-01
        • 2021-08-07
        • 2013-07-14
        • 2019-05-24
        相关资源
        最近更新 更多