The expanded average is grouped across multiple columns in pandas

I have a data framework that I would like to calculate a pop-up value over a single column (quiz_score), but you need to group two different columns (userid and week). The data is as follows:

data = {"userid": ['1','1','1','1','1','1','1','1', '2','2','2','2','2','2','2','2'],\
"week": [1,1,2,2,3,3,4,4, 1,2,2,3,3,4,4,5],\ 
"quiz_score": [12, 14, 14, 15, 9, 15, 11, 14, 15, 14, 15, 13, 15, 10, 14, 14]}

>>> df = pd.DataFrame(data, columns = ['userid', 'week', 'quiz_score'])
>>> df
   userid  week  quiz_score
0       1     1          12
1       1     1          14
2       1     2          14
3       1     2          15
4       1     3           9
5       1     3          15
6       1     4          11
7       1     4          14
8       2     1          15
9       2     2          14
10      2     2          15
11      2     3          13
12      2     3          15
13      2     4          10
14      2     4          14
15      2     5          14

, , . , shift() pd.expanding_mean() .expanding(). Mean() , - , , -, , :

df.groupby(['userid', 'week']). apply (pd.expanding_mean).reset_index()

, :

   userid  week  expanding_mean_quiz_score
0       1     1          NA
1       1     2          13
2       1     3          13.75
3       1     4          13.166666
4       1     5          13
5       1     6          13
6       2     1          NA
7       2     2          15
8       2     3          14.666666
9       2     4          14.4
10      2     5          13.714
11      2     6          13.75

, _mean_quiz_score / .

, expand_mean() .

+4
1

userid 'week' . expanding groupby . , , .

a=df.groupby(['userid', 'week'])['quiz_score'].agg(('sum', 'count'))
a = a.reindex(pd.MultiIndex.from_product([['1', '2'], range(1,7)], names=['userid', 'week']))
b = a.groupby(level=0).cumsum().groupby(level=0).shift(1)
b['em_quiz_score'] = b['sum'] / b['count']
c = b.reset_index().drop(['count', 'sum'], axis=1)
d = c.groupby('userid').fillna(method='ffill')
d['userid'] = c['userid']
d = d[['userid', 'week', 'em_quiz_score']]



   userid  week  em_quiz_score
0       1     1            NaN
1       1     2      13.000000
2       1     3      13.750000
3       1     4      13.166667
4       1     5      13.000000
5       1     6      13.000000
6       2     1            NaN
7       2     2      15.000000
8       2     3      14.666667
9       2     4      14.400000
10      2     5      13.714286
11      2     6      13.750000
+1

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


All Articles