My solution is marked incorrect although output appears identical to DataQuest's solution

Hi everyone,

I am struggling with my solution seemingly not matching what the dataquest lesson is requesting.
Lesson: Working With Strings In Pandas, Screen 11/13

My Code:

merged['IncomeGroup'] = merged['IncomeGroup'].str.upper()                          \
                                             .str.replace(r'(INCOME)?(:)?', '') \
                                             .str.replace(r'  ', ' ')

pv_incomes = merged.pivot_table(index  = 'IncomeGroup',
                                values = 'Happiness Score')

pv_incomes.plot(kind = 'bar',
                rot = 30,
                ylim = (0,10))

Solution Code:

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))

What actually happened:

Do any of you have an idea of what the difference is in the result of my code and the solution code?

This can be difficult to figure out, but the issue is that your current approach is not accounting for whitespace at the end of a string.

For example, if you run the following -


You will get -

Note the empty space after MIDDLE and before the ,.

So, you need to account for that whitespace as well.

1 Like

Thank you!! I added one additional Series.str.replace() line to catch those trailing spaces:

.str.replace(r'(MIDDLE )','MIDDLE')  \