Hurrah.
Making some real progress here.
There's almost an infinite way that you can view and select data with panders to head of frame.
We're only really scratching the surface here.
But once you learn these foundation steps you can really combine them in kind of any different way you
can imagine and really transform and view your data in a different light.
So let's keep going.
We're still viewing and selecting data at the moment.
So the next one I want to show you is paid a cross tab and this is a great way to compare two columns.
If you want to see them like a little innovative innovative is that the right word.
Not sure.
Doesn't really matter but you'll see a little comparative way.
Maybe that's a bit better word.
So cross tab is basically NSA say a pen is grab the make column.
Let's refresh our memory of what our data frame looks like.
We have make widgets car makes doors which is here.
So this is going to crossover the make column with the door column and so cross tab is called panders
dot cross tab and you pass it one column and then another column until we go in here.
These adores the amount of doors three four and five.
So we got three four five.
And these are the different makes here.
We got four different makes Toyota Honda BMW and Nissan beautiful.
So what this is doing is it's kind of aggregating these two columns together.
So let's explore this BMW.
There's one car with five doors Honda there's three cars with four doors.
Toyota is the only car brand that has three doors.
Not quite sure why that car would look like but who knows.
This is a kind of data you'll come across.
Okay now cross tab is great for comparing two columns but what if you wanted to compare more columns
in the context of another.
This is where a group buy comes in.
So let's put a little comment here.
Group by car sales.
I was going to say Petey I'm used to say NPD Group Buy and we want the make column
dot Main.
Now what do you think before we even run this code.
Now I'm going against my only rule here I've always run the code first.
What do you think this will do if we looked at this.
So we've got car sales which is our data frame that's up here we've got this little function here called
group buy we know it's a function because it's followed by brackets and then within the group by we've
got the make column we've got main had a think about it doesn't matter.
You should write the code out first and press shift and enter and see what happens.
So what this is going to do is it's going to group the data frame on the make column similar to what
our cross tab did.
But this time we have more columns here.
So it's given us the main values of the odometer of all the different types and makes as well as the
doors.
So BMW the main odometer rating is eleven thousand one hundred seventy nine.
And the main amount of doors is five.
And that makes sense because there's only one BMW car but again this group by you could group by almost
any column you can imagine and then call some operation on it as you go and you can imagine if you had
more columns here and you wanted to combine them in some way and view them you can use this group by
function to make sure you own the group it by a certain type.
Maybe you want only white Toyotas.
Maybe that's a little exercise.
See how you could group it by only looking at say two white Toyotas yeah two white Toyotas and then
seeing what happens from there.
But I just want to show you.
Group Buy it's kind of like cross tab but for focusing on more than one column at a time.
So what's next.
All right.
Well maybe but we have a look at some visualizations here.
That's a great idea.
So go.
Car sales odometer let's see if we can do a little plot here.
I'm going to call dot plot.
What do you think this will do.
Run that beautiful.
We'll get a plot of the different odometer value.
So this is what the beautiful thing about pandas is how beautiful and how quickly you can get an analysis
going and you might look at a table like this and not really be sure what's going on especially have
lots of rows but getting a visualization out is a great way to quickly understand what's happening and
we'll have a section coming up on how to make these visualizations a bit more interactive and a bit
more prettier with labels and different information here.
But what you might find is that if this plot doesn't show up you might need to run these two lines of
code.
And this is map plot lib in line because you see here this has generated this plot is actually remember
how we create an environment and then installed a whole bunch of tools for us.
Condit did that for us when we created our environment installed map plot lib because some of the functionality
like the plot function in pandas depends on map plot lib.
So behind the scenes Condor is actually set this up for us.
And when we imported pandas right up the top here we won't go right back up the top because that'll
take a little while but when we typed in import pandas as PD it might have done some work behind the
scenes for us to organize this plot but if it hasn't you can type in these two lines of code import
map plot lib not pi plot as penalty.
And this one here this percentage sign at the front is a little magic function you know books have different
magic functions which do a specific kind of thing.
All this is saying is plot our map plot leave plots inside the notebook so we can see them.
So if your plots don't show up make sure you've run these two lines of code so you can type them and
put them at the top of your notebook potentially if your plots aren't showing up.
So next thing we want to see is another kind of plot which is called a histogram.
Now histogram is a great way to see the distribution.
Distribution is a fancy word for spread of data.
So you imagine this.
Most of our odometer readings are between let's say this is 20 odd thousand up to 100000.
So the reason you can see this kind of curve shape here is because this is where the average about odometer
readings fall into and that these two values out here might be considered outliers because they're not
within this big clump of values.
So that's an easy way to run a histogram on your numerical columns.
Let's have a look at the price column we want to see what different prices of cars we have dot plot
her Now why is that.
What do you think that came up.
Do you remember.
No numeric data to plant.
Why is that.
Let's have a look at the time price dot D type.
We did dot D types at the start in one of the videos dot D type is just on a single column so d type
O for object.
So it's not numeric.
So our error is not numeric dated a plot.
What can we do here.
Now we could plot around for a bit and try to get this to work but let's have a look at how we might
research a problem like this but we might type in how to convert a panda's column price to integer
price column object to it in pandas.
Now this is a step you might take if you run into some kind of error.
We could always play around with this price column and try to fix it.
But I want to emphasize what what an actual workflow might look like when you're looking up something.
So you've googled how to convert a pan is common priced integer because that's what you want to do when
I click on this stack overflow.
I have a column called amount withholds values that look like this.
Now what you want to do is when you get to somewhere like Stack Overflow you want to see if this description
of this question matches our problem.
So they have a column called a mount which holds values that look like this at similar to us but ours
is called price.
When I do data framed up D times it returns this column as an object.
Ah so does ours.
How do I convert this to type int.
Yes that's what we want to do.
Let's have a look at some of these answers.
OK this is using Reg.
What you'll do with these answers is kind of look through them and see which one stands out to you the
most.
This green tech means that the question asker has marked this as the right answer to help them solve
their problem.
But sometimes there's other ones like maybe this one here I want to use one without regex cause rejects
kind of confuses me.
So this is also got 5 upvotes.
One of them is mine.
Previously in doing research for this video here.
So what you might do is copy this and so this you can see they've started off with a mount which is
like our price column and it's come out like this which is what we want to do we want to convert our
price column.
Let's have a look.
Car sales want to convert our price columns to something like what our odometer column looks like.
We've copied this piece of code from Stack Overflow.
We're going to type it in here but we're going to use it as reference right because we want to get practice
writing our own code.
So we're going to compare it to what ours is let's zoom out one little bit so we can see two sales at
once.
Excellent.
So this is saying data frame with a column amount equals data frame amount dot string dot replace some
interesting combination of code there with nothing as type int.
Now that's a fair bit in one guy but let's try replace the parts that we do know to our own data frame
our data frame is actually called car sales so we're going to type in car sales our market column is
actually called price so we'll type in price and we're going to reassign it to car sales price is what
this is saying is Hey grab the price column and set it equal to this.
Another car sales don't price column but let's put some adjustments.
So this means access axis this string and replace.
Now this little funky combination of slashes Dollar Signs comma and dots is saying hey find these symbols
within the price column and replace it after the comma replace it with nothing and then change the type
with as type 2 integer so let's go through that one more time.
Car sales price column.
We want to readjusted to equaling the car sales price column to keep the values the same but access
the string and replace the dollar sign commas and dots.
So dollar sign commas and dance with nothing.
So this is an empty string and then change it from a string so dot as type Integer.
Now that was a mouthful but remember.
If in doubt run the code shift and into.
Now let's see what our car sales data frame looks like.
Beautiful we've changed the price column to look more like the odometer column.
What's happened here.
The price column has included the Saints.
So maybe that's an exercise of how to fix that to not include the sense because we only want it in thousands
and this is increased it's are now the Nissan the White Nissan is now nine hundred and seventy thousand
dollars but let's see if we can plot it now price dot plot
beautiful so the plot is incorrect because the numbers are just way too high but at least we can plot
it now so the next thing we'd look at in terms of manipulating data is looking at this line of code
and going how can we readjust that.
So maybe that's a little exercise you could try try to readjust this line of code to not include those
last two zeros on the end and now we've seen a few different ways of viewing and selecting data some
of the major ones with pandas will now look at a few more ways that we can manipulate data so take a
little break if you need go back over the code we've gone through and I'll see in the next video.