A while ago I blogged about project Jupyter and in the last days I have been working a lot with it and I am still fascinated by its power.
Today I faced and solved two challenges I like to share here:
. plotting a pivot table
. changing legend entries
Assume we have the following dataframe:
Creating a pivot is a piece of cake by using the pandas pivot_table method on that dataframe:
|pivot = pd.pivot_table(df,index=["Org"],values=["Male employees","Female employees"],
This gets us
. the number of departments per org ( = len Female employees or len Male employees )
. the sum of male and female employees per org ( = sum Female employees and sum Male employees )
. as well as mean, min and max
How to plot ?
We can simply save the pivot tables as a new dataframe 'pivot' and call its plot method. Let's say we want to plot sum of male and female employees per org. First we need to drop the other statistics from the pivot table we don't need for the plot. Then we plot:
Only problem here is that the legend entries of this plot look a bit cryptic. Here is some code to fix this:
|ax = plt.gca()
handles,labels = ax.get_legend_handles_labels()
new_labels = 
for l in labels:
I have shared the entire notebook here.
1 blog comments below
I really like Excel but have always avoided pivot table. To tell the truth, I couldn't figure out the application. This is a brilliant explanation that is simple enough for wrapping my brain cells around. Thanks Amagard!
deanhills on Fri Feb 16, 2018 1:00 pm