‫All right.
‫Well, making some real progress here.
‫There's almost an infinite way that you can view and select data with pandas data 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 line.
‫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 PD Cross Tab.
‫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?
‫I'm 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 going to say, Hey, pandas, grab the mate column.
‫Let's refresh our memory of what our data frame looks like.
‫We have make, which is car makes doors, which is here.
‫So this is going to cross over the mate column with the door column.
‫And so cross tab is you call Pandas Cross Tab and you pass it one column and then another column.
‫So we go in here.
‫These are doors, the amount of doors, three, four and five.
‫So we've got three, four, five.
‫And these are the different makes here.
‫We got four different Macs, Toyota, Honda, BMW and Nissan.
‫Beautiful.
‫And 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 what that car would look like, but who knows?
‫This is 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 buy.
‫So we're going to go car sales.
‫I was going to say PD.
‫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 of 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 mate column.
‫We've got main.
‫Hmm.
‫How to 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 DataFrame on the mate 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 11,179 and the mean 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 could 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 only 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 their two white Toyotas.
‫Yeah, two white Toyotas.
‫And then seeing what happens from there.
‫But I just want to show you group by 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 we have a look at some visualizations.
‫Yeah, that's a great idea.
‫So we 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 to run that?
‫Beautiful.
‫We get a plot of the different odometer values.
‫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 is generated.
‫This plot is actually remember how we create an environment and installed a whole bunch of tools for
‫us.
‫Conda 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 will
‫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 pi plot.
‫As PLT and this one here, this percentage sign at the front is a little magic function.
‫Jupyter Notebooks have different magic functions which do a specific kind of thing.
‫All this is saying is plot our map, plot lib 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 the 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 100,000.
‫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, huh?
‫Now.
‫Why is that?
‫Why do you think that came up?
‫Do you remember?
‫No numeric data to plot.
‫Why is that?
‫Let's have a look at the data type.
‫Price dot D type.
‫We did D types at the start in one of the videos.
‫D type is just on a single column.
‫So D type o for object.
‫So it's non numeric.
‫So our error is non numeric data to plot.
‫Hmm.
‫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 pandas 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 to try to fix it.
‫But I want to emphasize what what an actual workflow might look like when you're looking up something.
‫So if Googled how to convert a pandas column price to integer because that's what we want to do.
‫We might click on this Stack Overflow.
‫I've a column called Amount with holds 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 Amount, which holds values that look like this that's similar to us,
‫but ours is called price.
‫When I do DataFrame 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.
‫Okay.
‫This is using regex.
‫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 tick 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 because regex kind of confuses me.
‫So this is also got five upvotes.
‫One of them is mine previously in doing research for this video here.
‫So what you might do is copy this.
‫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 StackOverflow.
‫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 cells at once.
‫Excellent.
‫So this is saying dataframe with a column amount equals dataframe, amount dot string, dot replace.
‫Some interesting combination of code there with nothing as type int.
‫Now that's a fair bit in one go, but let's try replace the parts that we do know to our own.
‫So DataFrame our dataframe is actually called car sales, so we're going to type in car sales.
‫Our amount column is actually called price.
‫So we'll type in price and we're going to reassign it to car sales price because what this is saying
‫is, hey, grab the price column and set it equal to this another car sales dot price column.
‫But let's put some adjustments.
‫So this means access the string and replace.
‫Now this little funky combination of slashes, dollar signs, comma and dots is saying.
‫Hi.
‫Find these symbols within the price column and replace it.
‫After the coma, replace it with nothing and then change the type with as type to integer.
‫So let's go through that one more time car sales price column.
‫We want to readjust it to equaling the car sales price column.
‫So keep the values the same, but access the string and replace the dollar sign commas and dots.
‫So dollar sign commas and dots with nothing.
‫So this is an empty string and then change it from a string.
‫So dot as type integer.
‫Woo.
‫Now that was a mouthful.
‫But remember, if in doubt run the code shift and enter.
‫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 are the price column has included the sense.
‫Hmm.
‫So maybe that's an exercise of how to fix that, not include the sense because we only want it in thousands
‫and this is increased.
‫It's a now the Nissan, the white Nissan is now $970,000.
‫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 that 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.
‫We'll 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 you in the next video.