avg
division
,什么是
Series
s = df.groupby(["hour"])["day"].nunique()
df_arr = df.groupby(["hour"]).size().reset_index(name='avg')
df_arr['avg'] /= df_arr["hour"].map(s)
#alternative
#df_arr = df_arr.assign(avg = df_arr['avg'] / df_arr["hour"].map(s))
print (df_arr)
hour avg
0 10 1.5
1 11 3.0
DataFrame
reset_index
g = df.groupby(["hour"])["day"]
df_arr = g.size().div(g.nunique()).reset_index(name='avg')
print (df_arr)
hour avg
0 10 1.5
1 11 3.0
mean
df_arr = df.groupby(["hour"])["day"].agg(['size','nunique'])
df_arr['avg'] = df_arr['size'] / df_arr['nunique']
print (df_arr)
size nunique avg
hour
10 3 2 1.5
11 6 2 3.0