So I hope you are ready to start digging in some data.
We're basically going to roll up our sleeves and build some pyramid population pyramids from scratch
this is gonna be a lot of fun.
So all the code by the way is in the resource folder.
So it's a link.
There's no downloadable there's no downloadable for this class.
You're not gonna get a giant notebook downloadable here is a link and we'll take you to an extract and
HMO page and your job will be to cut and paste it.
People I know say oh it's a lot of cutting and pasting but that's the best way to learn.
This is about learning here and you know if we're not going to learn more we know what's the point right.
So I find this to be to extract open it.
That's why I recommend you do and I recommend you open a brand new Jupiter notebook a Python 3 if possible
on your local machine.
And I'm going to exact the same things I'm going to cut and paste a code there and talk to you about
what we're doing every step of the way.
So of course before we can start to build these cool population pyramid we need data.
Right.
We can't do much without data.
So we're gonna go to the United Nations Department of Economic and Social Affairs population dynamic.
This site is there.
And so I think this page right here I mean make it smaller so we can see it so big thanks to the United
Nations to make this data available and this is the actual downloadable Page says Population Division
World Population Prospects 2019 and as a bunch of different options.
Make sure you choose C as we format and standard projections.
And if you scroll down you'll see some different flavors.
We're going to go with a population medium medium variant annual from 1950 to 2000 to 2100 This is perfect
for us.
They're breaking it in five year groups.
We perfect it will have basically five year group rose and they break into population by male and female
which is important for us and also the unit is in thousands.
So if you have a one it's really 1000 right.
Just keep that in mind if you're interested in translating that into actual units so click here click
the CSB format here and download it to your local machine in the same folder where you're gonna be running
a Jupiter notebook and let's pull it out so mine was named you know this is the name it gave and somewhere
I'm an important I'm pi and punt pandas and I called PANDAS Petey that read on the course CSP for this
file remember that's gonna be in the same folder where you're Jupiter a notebook is and I'm going to
look at the shape and the head basically the first five rows of the data it's always us we're going
to do very basic LDA exploration data analysis and this is the first step is the head and the shape
the shape tells us we have you know so back almost a million and a half of rows so quite a lot with
12 columns twelve features and ahead get us the first five columns first five rows I mean and we see
we have you know of interest to us we have the location which is the country the time which is the year
and we have the population male population female which what we going to need oh and also the age group
which is gonna be the groups the five year groups that's everything else we're going to throw out and
that's what we're gonna work with for now let's do a little bit more exploration.
So how many countries do we have.
That's right that's very easy to do.
We're gonna take the population location location here and we're gonna wrap it with a set.
Actually we don't need to do this and we're gonna wrap it we're gonna wrap it with a set then a length
the set is gonna make it unique it's gonna get the unique country names and the length is gonna calculate
the count how many it finds and look it at four hundred and forty.
Okay that's good to know.
So now let's see.
Let's take a peek at what kind of you know how are these countries and named so here I'm going to do
a same thing I'm gonna do the population location I mean wrap it with a set to get the unique names
then into a list and I'm going to sort that list calling sorted.
Right so a bunch of wrap we're wrapping it we're wrapping the series in it and a bunch of functions
so that we can get a list of unique names of countries enough for alphabetic order they're not actually
all countries some of them are going to be like the BRICS countries here you have all sorts of the Commonwealth
you'll have all sorts of different ways of grouping countries and continents or groups of countries
together with codes.
That's why we have four hundred and forty in here it's because there's all sorts of them even there's
even one for the world like a show that wanted to add in the intro video so it's a lot of countries
and if you look at the app that we're gonna build in that's definitely a decision that you can play
you can take on your own if you look at the if I click on here's the app that we're gonna build right
if I click on the country download the big bar it's gonna give you you know the typical your typical
scroll list and it's a list of four hundred and forty countries so that is a lot I know I understand
sounds a lot but I don't wanna remove anything cause I don't know what you want what you're looking
for out of this exercise so it's better for me to put everything and let you then limit that right.
So we're gonna have four hundred and forty in there but there's also sort of groups from continents
economic groups to countries.
Okay that looks good now let's see we know we have 440 let's do the minimum and a maximum date that's
always interesting as well so I mean actually make a few more rows here so that I make sure that you
can see it.
Here we go.
So here if we look closely we can see I'm doing a I'm taking the population pop def right.
I did a frame and then pull the time.
So this is the year MSA what is the uh the.
No I don't mean the smallest year and an update on Max the biggest year that we have and we'll wrap
that around a print and it's going to tell us our you know our lowest year and biggest year and we can
see it goes from 1950 to 2010.
So that's interesting.
I also want to know how many of these they are.
So that's a bit what we did before.
I'm gonna wrap this in a set that's going to give us the unique year animal wrap the whole thing in
length and that's going to give us a count of how many we have.
We have one hundred and fifty one.
So let's look at them.
I going to remove the length and you can see right.
This is basically goes year by year from nineteen fifty one by one year by year to two thousand one
hundred.
So this one I'm on the other hand I am going to limit in our app.
I don't want to be scrolling through one hundred and fifty years.
So if I click on the on a year button I just click on the button up there.
We're gonna click it to go to do it by 10 year chunks.
It's gonna make our lives a lot easier.
And because I'm filming this in 2020 it's perfect because it falls on 2020 as well.
So that's good.
Let's look at a few more things.
Let's take a peek at how we can pull specific data.
So I know one country in the list is called Japan.
You got to spell them exactly right.
Otherwise it's going to give an error right.
I'm not doing any error checking here.
This is something that if you wanted to upgrade this into a real commercial application you would have
to do.
But let me see let me just pull the location of Japan.
So if we just look at Japan on its own let me do this just to see what what it would return right.
So I'm going to take a subset when I say return
you know find everything from the in the data frame that has a look of the series location that's equal
to Japan and return all the rows that fulfill this condition.
So it's going to return a bunch of true false.
Right.
Let me run it one by one so that you can see it.
I'm not sure if you've done this before you'll return a series of true false and for every single row
in a data frame.
So we have one point four million rows and then what you do is you wrap that you're taking a subset
of your data set of where the location is equal to Japan and then it returns only rows with Japan so
we can see we have through some 3000 rows.
That's a Japan and you're gonna have every year right every year and every age group that's how kind
of how the data is is broken out.
So that's how we're gonna operate here.
I'm going to say give me a subset of everything that says Japan and that says 2020.
I wanted to put this current year on the population of Japan for this current year and I'm going to
sort it by this column AG GDP start basically is the order.
So we want the ages to be in ascending order that's the way the population pyramid is normally displayed.
So we want to we want to respect that and let's take a head.
So the head will give us a first few rows so we'll see the first row will be 0 to 4 for 2020 in Japan
and we see the number of males and females and so on and so forth.
Right so this is basically how we're gonna pull that data in order to create a pyramid.
Right.
Basically what that's what we're gonna go.
So I'm going to stop the video here and next video we'll start calling up some map lip and displaying
them.