In [87]: df.loc[df['A'].groupby(level='X').idxmax(), 'A']
Out[87]:
X Y
bar one -0.007381
baz four 0.210000
foo two 1.314373
qux one 0.716789
Name: A, dtype: float64
To find median values you can use
df['A'].groupby(level='X').median()
but it’s less clear which row should be associated with the median, because if the group has an even number of rows, the average of the middle rows is used to calculate the median. Thus, the median is not associated with one line, but rather two.
, , n//2 - ( (n-1)//2 - ),
grouped = df['A'].groupby(level='X', sort=True)
df.loc[grouped.apply(lambda grp: grp.index[grp.count()
, .
,
In [93]: df.loc[grouped.apply(lambda grp: grp.index[grp.count()//2]), 'A']
Out[93]:
X Y
bar two -1.219794
baz three -0.249321
foo two 1.314373
qux two 0.385795
Name: A, dtype: float64