Transposing a dataframe for an overlaid bar plot

Working off https://app.dataquest.io/m/288/guided-project%3A-investigating-fandango-movie-ratings/7/determining-the-direction-of-the-change, the goal was to get a plot like :

image

However, when you get to that point, you still don’t know that you need a dataframe that looks like :

2015 2016
mean 4.08527 3.88743
median 4 4
mode 4.5 4

What if your df looks like :

year mean median mode
0 2015 4.085271 4.0 4.5
1 2016 3.887435 4.0 4.0

How can you get it to the format that enables overlaid bar plots?

Hoping someone thinks of something more elegant :slight_smile: My solution :

final = summary.transpose()[1:]   # assumes that the first row in transposed is now 'year'
final.columns = summary['year']
# rest is copied from Alex O's notebook..
plt.style.use('fivethirtyeight')
final['2015'].plot.bar(color = '#0066FF', align = 'center', label = '2015', width = .25)
final['2016'].plot.bar(color = '#CC0000', align = 'edge', label = '2016', width = .25,
                         rot = 0, figsize = (8,5))
plt.title('Comparing summary statistics: 2015 vs 2016', y = 1.07)
plt.ylim(0,5.5)
plt.yticks(np.arange(0,5.1,.5))
plt.ylabel('Stars')
plt.legend(framealpha = 0, loc = 'upper center')
plt.show()
1 Like