Using groupby instead of for loop

I think I have found a MUCH easier way to create the graph we are being asked to mimic n the screen linked below. I shared my lines of code. Basically, I used .groupby() in order to group by the 'Region' column, and then isolated the 'Happiness Score' column and plotted that against the 'Region' column. I hope this helps! It yielded the EXACT SAME graph as the one in the explanation, without having to go through a dictionary and for loop, which I find confusing in this case.

Screen Link: Learn data science with Python and R projects

My Code:

regions = happiness2015.groupby('Region').mean()
score = regions['Happiness Score']
score.plot(x='Region', y=score, kind='barh', title='TITLE', xlim=(0,10)) 

I agree, using .groupby() is MUCH easier! However, I believe the purpose of this screen is to show you how to accomplish this task “manually” by using a for loop and a dictionary. The following few screens then introduce us to the .groupby() method. This is a classic teaching methodology and although it can be confusing at first, it provides a much deeper understanding of what the .groupby() method is actually doing for us behind the scenes.

That said, good job finding the easier way to do it! :smiley:

1 Like

Totally makes sense! I think starting by learning SQL was really helpful in conceptualizing stuff like this. I felt silly when I saw the next few screens lol.

1 Like

No worries…I feel silly ALL the time! :stuck_out_tongue_winking_eye: