I want to draw a boxplot of column Z
in dataframe df
by the categories X
and Y
. How can I sort the boxplot by the median, in descending order?
import pandas as pd
import random
n = 100
# this is probably a strange way to generate random data; please feel free to correct it
df = pd.DataFrame({"X": [random.choice(["A","B","C"]) for i in range(n)],
"Y": [random.choice(["a","b","c"]) for i in range(n)],
"Z": [random.gauss(0,1) for i in range(n)]})
df.boxplot(column="Z", by=["X", "Y"])
Note that this question is very similar, but they use a different data structure. I'm relatively new to pandas (and have only done some tutorials on python in general), so I couldn't figure out how to make my data work with the answer posted there. This may well be more of a reshaping than a plotting question. Maybe there is a solution using groupby
?
meds.sort(ascending=False)
tomeds.sort_values(ascending=False, inplace=True)
to make this work (Pandas 0.20.1, Python 3.6.1, Windows 8). – Pride