From the course: Python Data Analysis

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Solution: Country rankings

Solution: Country rankings - Python Tutorial

From the course: Python Data Analysis

Solution: Country rankings

- [Instructor] So we need to return a table of the top four countries in athletics, ranked by medal count. This is complicated enough that I will do it in steps, returning a partial result every time. CoderPad will continue to scold me, but too bad. First, I will group by medals. The array is called medals, and counts the occurrence of each country's name. Let's try this. Good. This gets me a series with a multi-index. Remember, we can move a level of the index to columns using unstack. Removing the first level, here it goes. Better. Good progress. I also want to get rid of those NaNs. They should be zeros. Next, we compute totals for each country by summing over the columns. Good. Last, we sort. And we take the first few. Perfect. Well done.

Contents