Groupby, count and average numpy, pandas in python

I have a dataframe that looks like this:

       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

I need to get a dataframe that has a unique userId, the number of user ratings and the average user rating, as shown below:

       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

Can anyone help?

+4
source share
3 answers
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
source

Drop it movieId, since we are not using it, groupby userId, and then apply the aggregation methods:

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)

What produces:

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

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
+1

Source: https://habr.com/ru/post/1674986/


All Articles