Combining Data With Pandas (09/11)

Screen Link:
happiness_scores_by_region_2015_2017|380x500

My Code:

happiness2017.rename(columns={'Happiness.Score': 'Happiness Score'}, inplace=True)
combined_2015_2016 = pd.merge(how='outer', on=['Country', 'Region'], left=happiness2015, right=happiness2016, suffixes=('_2015', '_2016'))
combined_2015_2016_2017 = pd.merge(how='outer', on='Country', left=combined_2015_2016, right=happiness2017, suffixes=('', '_2017'))

combined_2015_2016_2017.rename(columns={'Happiness Score': 'Happiness Score_2017'}, inplace=True)

summary = combined_2015_2016_2017[['Country', 'Region', 'Happiness Score_2015', 'Happiness Score_2016', 'Happiness Score_2017']]

pivot_table_summary = summary.pivot_table(values=['Happiness Score_2015', 'Happiness Score_2016', 'Happiness Score_2017'], index='Region', aggfunc=np.mean)

pivot_table_summary.plot(kind='bar', title='Happiness Scores by Region through 2015~2017', legend=True, figsize=(10,10))

plt.xticks(rotation=80)

JUST TO BOAST MY WORK

I was suddenly wondering how the ‘Happiness Score’ got changed by ‘Region’ through 2015~2017 and contemplated for a while and made it! So happy and proud of myself :slight_smile:!

1 Like