Hello everyone.
Welcome to the day to capstone project solutions lecture this lecture we're going to go over the solutions
for the night when one calls it a capstone project.
Let's go out and jump to Jupiter notebook to get started.
All right.
Here I am at the notebook.
Let's go ahead and get started.
First thing I want to do is important some pie and pandas when we go out and say important umpires and
P and import pandas as PD and the next cell asks you to import visualization libraries and set map pilot
in line.
This was a little vague kind of on purpose but hopefully you realize it's just talking about that.
LIBMAN seaborne so I can say in part but PI plot as Piazzi import seaborne as Asinus and then I'm going
to go out and say what live in line.
Next we wanted to read in the the file as a data frame called The F.
So PD that read C S V and we can actually just call 9 and then see nine on one that v is the file we're
concerned about.
And then we want to check the info of the data.
So the info.
So this tells us there's almost about 100000 entries here and there's nine total columns.
And then we actually have already the column descriptions appear in the notebook then we want to check
the head of the DEA.
And we can do that just by calling the method in the head.
And here we have the head of the earth so he can get a sense of what this actually looks like Lotusphere
the title has some sort of label immerse or fire colon and then the actual title event and we have a
time stamp GWP which is a township and we scroll over to the right.
We have an address and then we call them.
Next we want to answer some basic questions such as What are the top five zip codes for 911 calls.
Well we go ahead and grab the column and then we can actually just specify value counts off of this
and that or turn all the counts for all the zip codes.
And we just want the top five.
So we just call had 5 off that series.
And here we have the top five zip codes.
Next we want to figure out what are the top five township's PWP for 911 calls.
You can do a very similar thing specify DWP call value accounts and then head to 5 and this will get
you these results so lower Merion Abbington etc..
Now we want to take a look at the title column and how many unique title codes are there.
There's two ways of doing this you can say the title call unique audits and the return an array of all
the unique titles mean minicon just check the length of that array and get back the number or to do
this all in one step.
You can just call and unique.
They'll also return 110.
OK next step we want to create some new features in the titles column.
There are a reason slash Department's code specified before the title code.
And these are M-S fire and traffic.
We want to use the apply a custom land expression to create a new column called reason that contains
the string value.
So for example if the title column was M-S back pains last injury the reason column for that value would
be M-S we go ahead and take a look back at the head.
Like I mentioned before there is some sort of reason here colon.
And then the full title M-S fire.
And then later on there's traffic.
Let's go ahead and create this reason column using apply.
First off let's go out and break down the logic of what we want to do.
I'm going to grab from Title an example.
I ellos see it is kind of the way I like to do it and then set this as X and see what do I have to do
to X to actually grab this title here.
Well since this is a string I can say that splits by colon.
And then I notice that the title always comes first so if I just grab the first element off of that
list.
Now I have this reason here.
All right.
This is the kind of thing we're going to do.
Ferland expression is going to show that to create the column reason set it equal to the title column
the apply and then say lambda the title and say title dot split based off of the colon and then I'm
going to grab the very first item in that list.
Let's go ahead and check out what reason looks like.
And that's what it looks like we are being Merse fire traffic.
Perfect.
All right.
Hopefully that wasn't too hard can be a little tricky to express a land expression correctly apply a
nice little hint for this.
Just grab an example and then see what you have to do to that example and then convert this into the
land expression.
Now we want to know what are the most common reason for you and I don't want to call based off this
new column.
Well that's actually quite easy.
We can just call reason value counts that returns that EMI is the most common reason if you only want
to return the most common reason you could just say had one and it would return M-S but says there's
only three values we're going to show all of them.
Next up we wanted to use seaborne to create a counterplot of nine one one calls by reason.
OK.
We're going to want to say Asinus counterplot and then we want to specify the x axis to be reason here
.
So when I say X is reason then for counterplot there is no Y is the Y is automatically chosen to be
just a count.
So we're going to say data is.
And then run this.
And here we have our count plots.
Now don't worry if you didn't get the exact coloring as shown in the solutions book.
That's just a little extra call on the palette.
So you could do something like Palette equals and the one I used for the solutions was just below this
and that changes the color a bit.
But don't worry about the color matching up or up.
That's kind of an optional Color Mapping for each choose.
Hopefully we're able to figure out that count plot equals reason and that equals the f are the most
important parts of creating that count plot.
Now let's go to focus on the time information.
What does the data type of the objects in the time stamp call them.
Well what you can do is just call DFT info and it will actually tell you if you look at time stamp that
it's a object type and this kind of gives you a hint that it's going to be not a number and it's actually
a string.
And if we want to confirm that we can just grab one of those objects so we can say time stamp ILO's
C02 scrub the first one and note that we get back a string.
We want to further confirm that we can say type and here we have as TR for string.
Something to know if you just go all the way up to the top.
This is confirmed right here with a string variable.
Ok guns roll back down.
So we know we have times information but it's actually a string.
Now we want to do is turn these timestamps into date time objects.
We want to do that using PD to date time and you can to click this link.
You'll notice here to link to documentation on how to use to underscore date time.
Go ahead and just check out the examples here but it's actually quite a simple call so hopefully you
figured it out but it's basically just saying PD to underscore that time passing the column we want
to convert this case it's time stamp.
And then I'm just going to reset that column to be equal to this.
And now if I go ahead and take a look at this code right here.
Copy and paste it.
Now I have a time stamp object which is exactly what we want.
This is a Date Time object now.
All right.
Now that it's a date time object or a time stamp with pandas you can actually grasp specific attributes
from the object by calling them.
And there's a little piece of example code here let's go and check that out.
I just copy and paste this example code.
We can run this and show that we can actually grab information off of this and then it says use Jupiter's
tab method to explore the various attributes you can call.
And now the time column or actually date time objects use that apply to create three new columns called
our month and day of week.
You will create these columns based off the time stamp column and then you can reference the solutions
if you get stuck on that.
Let me go and break this down just a little further.
But ahead and take a look at what time is time.
Is that very first entry in this time stamp column.
No it says time stamp.
2015 12 10 in the hour minutes seconds.
I do dots and call attributes off of this object.
Notice I have a wide variety of attributes I can call her instance if I want to know the actual year
.
This is in I can just call year and report back the year as 2015 so I want to know what hour of the
day it was.
Reports it back as seventeen hundred hours.
So in military or a 24 hour format I want to know for instance the month that was in and called month
now I can even call things like day of week so day of week is three.
And later on there's actually a dictionary here for you to reference.
All right.
Now we want to do is have this ability of client attributes off at this time same column to actually
create in our month and day of week column we're going to show you how we're going to do that.
We're going to say for instance the half hour is equal to diea of the time stamp column.
And we're going to say apply we're saying Lamda take the time and then return the hour attribute.
We go in and run that and then if we check out the our attribute now we have all the hours where the
actual call occurred off the time stamp and we're going to the exact same thing for month and day of
week.
You can actually just copy and paste this line of code and instead of our For the first one we'll go
ahead and put in months and change this attribute to the month call and for the second one we're going
to go ahead and call this day of week
and then change this to the day of week call.
Now we go ahead and run this.
Let's go ahead and check out the head of our data frame now just to confirm that this all worked.
And if we scroll all the way to the right notice here we have three new columns.
We have our month and day of week which some numbers are present that.
OK the next thing you wanted to notice was how the day of week is integer from 0 to 6 that it says use
the dot map method with this dictionary to map the actual string names to the day of the week in which
you have to do here.
Go ahead and copy this line of code.
Now we have the map and then all you have to do is just map to see say the IF day of week equals the
f day of week and then you can actually say that map which is the method they're referring to here.
And then passen the map.
And now if we check out the head of our data frame
notice if we scroll all the way to the right again day of week is now the actual string Thursday-Friday
cetera.
So it's a little easier to read.
OK.
Now we want to you see more and create a counter plot of the day of week column with the hue based off
of the reason column.
Let's go ahead and recreate that.
We can say Asinus counterplot X is going to be the day of column
data is going to be día.
Remember we don't need to pass in a week or just counting.
And if we just do this we get the total count per day of week.
So we can see that there's a bit of a drop on Sunday where we can add to this is the Heugh specify that
the hue is by reason and we have a recreation of the plot shown in these solutions.
Now something to note here is the color palette is a little different.
If you want to use the same color palette I did.
You can go ahead and say palette Zucco to viridis and that reproduce the same color palette.
And then the last thing to note is that the legend is actually out inside here and in solution's we
want to relocate the legend.
You can actually just copy and paste the same code you did during the Seaborn lectures where we actually
show how to put the legend outside.
We're going to run that.
Now we have the legend outside of the plot itself.
All right.
Now the next task we had to perform was the same for the month column.
And basically all you have to do is you can just copy and paste this code and change x to be month and
you'll have the exact same plot except it's for the month column and then the next question was Did
you notice something strange about this plot.
And hopefully you're able to notice that you are actually missing some months.
It goes one through eight but then you don't have months 9 10 and 11.
They go straight to 12.
So now that we have noticed that missing information what we may want to do is try plotting the information
in another way possibly through a simple line plot that fills in those missing months and in order to
do that we'll need to do some additional work with pandas.
We'll go ahead and start part one of the solutions lecture here and carry on in part two by creating
a group by object called by month in order to try to solve this issue.
Okay.
Thanks everyone and I'll see you at the next lecture.
For part two of the solutions