I have dataframe
df = pd.DataFrame({'member_id': [111, 111, 111, 111, 222, 222, 333, 333], 'event_duration': [12, 242, 3, 21, 4, 76, 34, 12], 'period': [1, 2, 2, 2, 3, 3, 4, 4]})
event_duration member_id period
0 12 111 1
1 242 111 2
2 3 111 2
3 21 111 2
4 4 222 3
5 76 222 3
6 34 333 4
7 12 333 4
I need to count number of period to every member_id and median of periods
I use
res = df.groupby(['member_id']).agg({'period': pd.Series.nunique, 'event_duration': np.median}).reset_index()
But it print median to all period. But I need, for example for 111 get median to 1 and 2 period, [12, 266], how can I do that?