- [Instructor] So, let's get started
on the challenge for chapter six which is indexing.
So, let's import Pandas,
let's read the Olympic CSV file into our DataFrame
called 00, so plot the total number
of medals awarded at each of the Olympic Games
throughout history, so since we want
the number of medals throughout history,
let's use the edition, so 00.Edition
which is the series in the 00 DataFrame
and let's use value_counts.
And if we plot that,
we don't seem to have a plot that we expected,
so what seems to be wrong here?
Well, let's take a few steps back
and look at the value_counts object.
Now, if I do a type here,
we can see that 00.Edition.value_counts
is in fact a Pandas series,
so what that means then
is that when I do a value_counts,
the first column is the index
and the second column are the values for that index,
so part of the reason that we had a problem
when plotting that graph
was this index is not sorted,
so what we can do then is to sort the index
of these value_Counts because remember,
this is a series,
so, sort_index
and let's look at the resulting structure
and now we can see that we've got the series sorted
by year, and so, then,
if we go ahead and do a plot,
we get the total number of medals presented
at each of the Olympic Games throughout history.
So, which countries did not win a medal
in the 2008 Beijing Olympics
and how many countries were there?
So, let's think a little bit
about how we will tackle this problem.
Our approach will be to get a common index
of NOC or the country,
and we'll get the list of all of the countries
and index them using the NOC code.
So, if you visualize a column
with all of the countries listed there,
and what we're going to then do is we're going to take a list
of all of the countries that won a medal in 2008
and overlay that on that column
with all of the countries.
Where we have missing data
is where a country has not won a medal.
So, let's identify the countries that won a medal in 2008.
So, 00, which is our DataFrame,
so 00.Edition
and 2008, equals 2008
and let's store this in the DataFrame last Olympics.
Now, this is another really good reason
to familiarize yourself
with your original dataset.
If you remember, the data from The Guardian
had a tab of all of the Olympic committees or countries,
so let's grab that from The Guardian site.
This file will also be in your data folder
in the exercise file.
So, I've done a search for Summer Olympics Medal Data,
get the data
and I'll download the full spreadsheet.
So, this is the tab that I was referring to,
the IOC country codes.
We're going to need this information,
so let's save this as a CSV file.
And I'm going to copy this across
to my data folder.
Now, let's read that CSV file into a DataFrame.
Let's call that DataFrame NOC for the Olympic countries,
so pd.read_csv.
That's in the data folder
and I'm going to just leave the original file name
and let's use the DataFrame name NOC here
and let's just make sure that we have inputted
the data that we expect,
so NOC.Head
and you can see that the first column is the country,
the second column is the Olympic Committee code
for that country, the ISO code
and finally, country.1.
Now, because I haven't seen this dataset before,
I probably would want to know
what's the difference between what's in the country column
and the country.1 column.
Is there anything that we need to understand
about the differences between these columns?
So, firstly I want to just check
if there is any difference,
so I'll do an NOC country
and I want to see if there are any rules
where the country and the country.1 differ,
so NOC and country.1
and so, I'm checking to see if NOC is not equal to country.1
at any of the rows
and I want to display those results.
And given that this is empty,
we can see that there is no difference
between country and country.1
and so, we don't need to be overly concerned
about the fact that there's this additional column
called country.1.
So, let's set the NOC DataFrame to have an index
of the Olympic Committee code.
So, I see NOC and let's set the index
to be the series name
and let me just copy that series name from here.
And I want to store this inplace,
so inplace equals true
and let's have a look at our DataFrame
with its new index.
So, in the next step,
you want to get a unique list
of all of the countries
and we can do that using value_counts,
so our original DataFrame from 2008
is LO, the NOC's there and value_counts
and remember that this is going to be a series,
so we'll just store this in the series name medals_2008
'cause this gives us the medals that have been won in 2008.
Let's just check that
and now what we're going to want to do
is to add a new series
to our DataFrame NOC
which has the results of all of the medals won in 2008.
So, we have NOC
medal 2008
and we're going to set the new series that we've created
to that DataFrame,
so let's have a look at our NOC DataFrame.
So, here wherever we have a nan value or a missing data
is where the country did not win
any medals in the 2008 Olympics.
So, if you want to get a list of that,
we say NOC medal 2008
is null
and let's return that DataFrame.
So, here we have it.
These are all of the countries
that did not win a medal
in the 2008 Olympics.
In the next video, we will look at Groupby.