【问题标题】:groupby, count and average in numpy, pandas in pythonnumpy中的groupby,count和average,python中的pandas
【发布时间】:2017-09-13 08:19:28
【问题描述】:

我有一个如下所示的数据框:

       userId  movieId  rating
0           1       31     2.5
1           1     1029     3.0
2           1     3671     3.0
3           2       10     4.0
4           2       17     5.0
5           3       60     3.0
6           3      110     4.0
7           3      247     3.5
8           4       10     4.0
9           4      112     5.0
10          5        3     4.0
11          5       39     4.0
12          5      104     4.0

我需要得到一个数据框,它具有唯一的 userId、用户的评分数和用户的平均评分,如下所示:

       userId    count    mean
0           1        3    2.83
1           2        2     4.5
2           3        3     3.5
3           4        2     4.5
4           5        3     4.0

有人可以帮忙吗?

【问题讨论】:

    标签: python-3.x pandas numpy jupyter


    【解决方案1】:

    删除movieId,因为我们不使用它,groupby userId,然后应用聚合方法:

    import pandas as pd
    
    df = pd.DataFrame({'userId': [1,1,1,2,2,3,3,3,4,4,5,5,5],
                      'movieId':[31,1029,3671,10,17,60,110,247,10,112,3,39,104],
                      'rating':[2.5,3.0,3.0,4.0,5.0,3.0,4.0,3.5,4.0,5.0,4.0,4.0,4.0]})
    
    df = df.drop('movieId', axis=1).groupby('userId').agg(['count','mean'])
    
    print(df)
    

    产生:

           rating          
            count      mean
    userId                 
    1           3  2.833333
    2           2  4.500000
    3           3  3.500000
    4           2  4.500000
    5           3  4.000000
    

    【讨论】:

    • df.drop('movieId', axis=1).groupby('userId').rating.agg(['count','mean']) 清理该多索引。加一
    【解决方案2】:
    df1 = df.groupby('userId')['rating'].agg(['count','mean']).reset_index()
    print(df1)
    
    
       userId  count      mean
    0       1      3  2.833333
    1       2      2  4.500000
    2       3      3  3.500000
    3       4      2  4.500000
    4       5      3  4.000000
    

    【讨论】:

      【解决方案3】:

      这是一种基于 NumPy 的方法,使用 userID 列似乎已排序这一事实 -

      unq, tags, count = np.unique(df.userId.values, return_inverse=1, return_counts=1)
      mean_vals = np.bincount(tags, df.rating.values)/count
      df_out = pd.DataFrame(np.c_[unq, count], columns = (('userID', 'count')))
      df_out['mean'] = mean_vals
      

      示例运行 -

      In [103]: df
      Out[103]: 
          userId  movieId  rating
      0        1       31     2.5
      1        1     1029     3.0
      2        1     3671     3.0
      3        2       10     4.0
      4        2       17     5.0
      5        3       60     3.0
      6        3      110     4.0
      7        3      247     3.5
      8        4       10     4.0
      9        4      112     5.0
      10       5        3     4.0
      11       5       39     4.0
      12       5      104     4.0
      
      In [104]: df_out
      Out[104]: 
         userID  count      mean
      0       1      3  2.833333
      1       2      2  4.500000
      2       3      3  3.500000
      3       4      2  4.500000
      4       5      3  4.000000
      

      【讨论】:

        猜你喜欢
        • 2021-11-20
        • 1970-01-01
        • 2018-09-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-15
        • 1970-01-01
        • 2021-12-27
        • 1970-01-01
        相关资源
        最近更新 更多