In the last video we created this bad boy here.
Now let's try and make something similar but using our own data so we can practice a little bit of data
manipulation along with plotting and put some of our pandas in map plot lib skills together.
So what we'll do we'll format the price column first we'll get it into integer format because right
now we don't want those dollar signs and columns and commas we don't want those for when we're doing
numerical operations.
So to do that we'll go car sales and we'll change the price column.
We need to access the string we saw this in the pandas section.
String dot replace.
And now we want to pass it a little bit of regex here.
There we go.
So essentially all this is saying is get the dollar sign the comma and the DOT which is here and here
and replace it and we're going to replace it with an empty string.
So we'll see what this does.
Car sales we need to add car sales there so let's see what it looks like now.
OK.
Beautiful.
But we've got two extra zeros on the end so we don't want that.
So we want to go remove last two zeros.
We've got car sales want to access the price column again.
We're going to reassign it to car sales price.
Again it's still a string.
So if we have a look at this guy here type just to exemplify what we're doing with the DOT string it's
a series.
OK.
But that's right.
Well this is still a string.
So that's we're accessing it with the DOT string method here.
Maybe if we go 0 string there we go.
So that's accessing this little one here and it's telling us that it's a string.
So that's why we have to access it as a string first with DOT SDR.
And now we want to remove the last two using slicing.
Let's have a look at what car sales looks like here.
Oh no.
We've removed too many.
So what we can do is rerun it.
We're gonna have to re import our data frame to fix that little issue that's going to just reset it.
Because remember Jupiter sales are running in sequential mode so if we run this sell more than once
it's going to continually remove the last two digits of our price column as long as we keep running
this.
So we don't want that.
So now our price column is in the format that we want we're going to add a sale date column which will
be similar to the PD date range function that we used up here paid a date range so let's do that data
range.
1 1.
We want to get past the date 2020 number of periods.
So this is how many dates we want to add.
We've got periods here.
So that's a parameter.
So that's going to add this one.
We had a thousand periods.
What we don't want that many for our car sales data frame.
We actually just want date period for each row here.
So we can do that by going periods Eagles Lane car sales and then we'll check our car sales data frame
again.
Beautiful.
So now we've got a sale date here which is sequential.
That's okay.
Maybe they all weren't sold on sequential dates but we're just making an example of how we can start
to manipulate our existing data frames.
So the next thing what we want to do is we want to make a total column.
So we want to start to add up how much we've made from selling all our cars.
So if we added 4000 to 5000 to 7000.
So this is where we can use something like Come some because it's going to add each of these prices
cumulatively so let's make a total column.
This should be car sales.
Total sales and then what do we want we want the price column.
That's right.
Car sales rise.
Dot com some.
Let's see what happens here.
Car sales Oh what have we done.
It's worked.
But because our price column is still in a string format it's just concatenated the strings to each
other.
So let's have a look.
Car sales price zero daytime time Oh that's a bit boring.
Let's get it back to exemplifying that it's a string.
There we go.
So our price column is still a string.
So to fix this we have to change it first into an integer and then call the come some function so we
can do dot as type int dot come some.
So what this is going to do is go reassign the total sales column to be the price column as an integer
as type int dot cumulative sum.
This should work in theory but as always run the code then check Beautiful.
OK.
So this has gone total sales of thousand plus five thousand is nine thousand plus seven thousand is
sixteen thousand and then it keeps going right up to the end so after we've sold all of our cars across
these 10 days or so we have seventy six thousand four hundred and fifty.
Beautiful.
That's some good profit there.
You can use that to fund.
About a store.
And now let's plot the total sales so we want car sales we want to see what our total sales looks like
over time.
So kind of replicating this graph here with our own data.
Car sales dot plot x equals sale date and now this way you can cold up plot directly on a painter's
data frame this is Panda's version of the map partly API or the wrapper Panda's wrapper for the map
partly of API.
So we're just going.
Data frame dot plot and we're passing it out X which is the date and now y which is the total sales
column.
So let's see what this looks like.
Total sales.
They'll put a semicolon at the end so it doesn't print out beautiful.
There we go.
So this is just an example of how you can take some code write from the documentation or some example
that you found online and we've replicated it up here but then we've taken the principles from it or
some of them and put them together with our own data and then created this little visualization here.
So now we can visualize.
Okay so say we sold our 10 cars over the first 10 days of January.
Now our total sales go on up into the riot which is very pleasing for our boss because that's the type
of graph I want to see profits up into the right and so hopefully you starting to get an idea of the
kind of workflow that you can go about when you're working on your own projects or working on problems
you don't necessarily know the answer to.
It's all about searching up trying to find some kind of example taking information from that and then
seeing if it works with your own problem.
Now we've seen a line plot.
How about we try scatter plot so we've got car sales.
Plot X. Let's do it with the odometer this time a domino column.
K M beautiful.
Is that the right column name.
Yep we've worked with this data frame.
Also kind of familiar with what's happening price and then when you're calling the plot function there's
a little parameter here called kind which we can feed the type scatter to hopefully get a scatter plot
here.
What's happening scatter requires y column to be numeric is our price column not numeric.
It's still not numeric because here we've called as type int.
So this is only going to turn the price column into an integer for running this single line of code.
It's not going to reassign the price column to be an integer.
These are still strings in the car sales data frame.
So what we can do to make this work we want to reassign the price column to be an integer price an Eagles
sales price as tied in because our prices are always going to be integers.
Money is a numerical value so it's okay to reassign this data type to be integer they should work.
So we got reassign the price column to int plot scatter plot with price column as numeric.
So without this line of code we'll comment that out we're going to get an error here.
Value era requires y column to be numeric but what we're doing with this line here we uncle meant it.
To comment and on comment code we can do command Slash.
So I'm just pressing command slash here.
We're going to reassign it as a numeric column.
So if we do this beautiful.
Now we have a scatter plot of our odometer column versus price.
And remember to remove this little piece of code here you can put a semicolon right at the end Excellent.
Now I'm kind of seeing a few examples here of how we can start to plot directly from a panda's data
frame.
We'll see a few more but we'll end this video before it gets too long.
So in the meantime before the next one maybe you'll try plot a couple of these columns together whatever
kind of plot you want to do.
You've seen a few.
A bar graph of scatter plot a line plot to have a practice of that and I'll see you in the next video.