It seems that the answer proposed in mission “Working With Strings In Pandas”, point 11 (Challenge: Clean a String Column, Aggregate the Data, and Plot the Results) is incorrect.
The code below results in extra space between the words in strings ‘HIGH OECD’ and ‘HIGH NONOECD’:
merged[‘IncomeGroup’] = merged[‘IncomeGroup’].str.replace(’ income’, ‘’).str.replace(’:’, ‘’).str.upper()
pv_incomes = merged.pivot_table(values=‘Happiness Score’, index=‘IncomeGroup’)
pv_incomes.plot(kind=‘bar’, rot=30, ylim=(0,10))
plt.show()
I’ve dealt with it by adding additional replace string method (to replace double space with single one):
merged[‘IncomeGroup’] = merged[‘IncomeGroup’].str.replace(‘income’,’’).str.replace(’:’, ‘’).str.replace(’ ', ’ ').str.upper()
pv_incomes = merged.pivot_table(values=‘Happiness Score’, index=‘IncomeGroup’)
pv_incomes.plot(kind=‘bar’, rot=30, ylim=(0,10))
plt.show()
Please let me know what do you guys think about it.