grouped = df.groupby(['X', 'Y'])
df2 = pd.DataFrame({col: vals['Z'] for col, vals in grouped})
meds = df2.median()
meds.sort(ascending=False)
df2 = df2[meds.index]
df2.boxplot()
plt.show()
How can I sort a boxplot in pandas by the median values?