在Python的NumPy和Pandas中,如何使用groupby、count和average?

6

我有一个数据框,看起来像这样:

       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

我需要获取一个数据帧,其中包含独特的用户ID、用户评分数量以及用户平均评分,如下所示:

       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

有人可以帮忙吗?

3个回答

6

由于我们不再使用movieId,因此将其删除,按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

1
df.drop('movieId', axis=1).groupby('userId').rating.agg(['count','mean']) 为了清理多重索引。Plus One - piRSquared

6
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

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