‫Welcome back.
‫We've had a look at a few different ways of describing data that we've imported two pandas into a data
‫frame.
‫Let's have a look at a couple of different ways we can view and select data.
‫So we'll come down here, we'll put another little heading in.
‫So we go viewing and selecting data.
‫We're going to hit escape and M for markdown shift and enter because we're keeping our notebooks nice
‫and clean and communicative right from the beginning.
‫So to begin, one of the first things you'll do is have a look at the head of your data frame.
‫Now, remember, this has brackets after it.
‫So it's a function.
‫And what this is going to do is return the first or the top five rows of your data frame.
‫Now, you might be thinking, why five?
‫Well, that's a good question.
‫I'm not entirely sure, but five seems like a pretty good number.
‫And so in practice, what you'll probably be doing is manipulating your data frame in some way and then
‫calling head on it fairly often.
‫So you don't have now our data frames only ten rows.
‫So calling the whole thing isn't actually too bad.
‫But imagine if you had thousands of these.
‫So what head does is it gives you a quick snapshot in a small little space of what your data frame contains.
‫So if you make a couple of quick changes here and there, you might want to look at the top five rows.
‫And if five isn't enough, maybe you want to look at the top seven.
‫While the beautiful thing about head is that you can type in a number here and it will return that many
‫rows so you can have a play around and return whatever amount of rows you want.
‫Now, for some reason, if you wanted the bottom of your data frame, you can use dot tail and that
‫will return.
‫You might have guessed.
‫The bottom five rows.
‫Now, this is handy if you're doing some alterations on the bottom of your dataframe rather than the
‫top, and so the changes will only appear towards the bottom.
‫Something like that entails much the same.
‫You can put in any kind of number you want here.
‫Press shift and enter and it will turn the bottom three rows there.
‫Now the next two functions that we're going to look at are lock and I lock.
‫So let's write them down here.
‫Lock and dot.
‫I lock.
‫Now, before we go into the details, remember, we always want to run code first.
‫If and out run the code.
‫That's our motto.
‫So let's create a series here so we can demonstrate the difference between Lock and I lock.
‫So I'm going to make one called animals.
‫What's your favorite animal?
‫Maybe since we're using pandas, maybe we'll put a panda in there and one more snake.
‫Now we're going to set the index here.
‫Now, if we created a series by default, the index will be zero too, however many things we have here.
‫But let's actually see that rather than talk about it.
‫Because remember, if in doubt run the code animals 0 to 4 in order.
‫But if we wanted our own custom index, which we can do by passing the index parameter and then we put
‫in a list of say zero three, now we want this index to be out of order on purpose so we can demonstrate
‫lock and I lock.
‫There we go.
‫So we have five things here, five things here, both python lists.
‫Now, if we run this, we can see that our index has changed.
‫So let's have a look.
‫Animals Dot Locke three.
‫If we call this, what do you think?
‫Will come back and lock?
‫You can consider a short for location.
‫So let's try shift into dog and snake.
‫Beautiful.
‫So what Locke refers to is the index numbers.
‫So because we have three and three here, it returns two items.
‫Let's try animals.
‫Lock nine.
‫What do you think this will come back with?
‫Which animal?
‫Shift into third.
‫The reason being is because bird is at index nine.
‫So we created bird.
‫One, two, three, one, two, three.
‫Index nine.
‫Okay, now let's try our car sales data frame.
‫If you're not sure what this looks like, we'll run it here so we can see.
‫Okay.
‫Zero one, two, three.
‫The index is still in order here.
‫Beautiful.
‫Now let's run.
‫Lock on this dot.
‫Lock three.
‫What do you think?
‫This will come back with?
‫Which car?
‫Let's try it out.
‫Woo beautiful comes back with position three.
‫A beautiful black BMW for $22,000 can be all yours.
‫I could become a car salesman.
‫What do you think?
‫So now let's try another one.
‫Let's try eye lock.
‫This time we'll put a log here.
‫So we go animals dot I lock three and this time we're doing eye lock instead of just lock.
‫So if we have five items here, what do you think this will come back with?
‫Let's try it out.
‫Shift into Panda now so we can see it.
‫Let's put a new cell here and go.
‫Animals.
‫Beautiful.
‫So what I like refers to I like refers to position.
‫So we can see here.
‫Zero, one, two, three.
‫If this was in order.
‫Remember Python lists and data frames and series start from zero.
‫I lock refers to position whereas lock refers to index.
‫Let's try maybe with our car sales data frame.
‫Car sales.
‫I lock three.
‫Ah.
‫It comes back with the same as Locke.
‫That is because if we go up.
‫Well, actually, let's just do it here.
‫Car sales.
‫If we have a look at our data frame, I lock.
‫Position three remember because I like refers to position so 0123 is the same as index three.
‫So that's the main two points you have to remember about ie lock and lock is that I lock.
‫Refers to position and we'll put in here lock.
‫Lock refers to index.
‫They're the main two differentiations there.
‫Okay.
‫Now, the beautiful thing about this is that with eye lock and lock, you can use slicing.
‫So if you've ever used Python less, you might be familiar with slicing, but if not, I'll show you
‫what that looks like.
‫So we might type in animals.
‫I lock.
‫Colon three.
‫And what this means is, rather than talk about it, let's run the code.
‫Is give us the items in animals up to position three.
‫Now I like doesn't include position three so it'll give us remember start from zero zero 1 to 3.
‫So it's going to give us up to three.
‫Now let's try it with the car sales data friend.
‫Car sales dot lock, colon three shift and enter.
‫There we go.
‫It's given us up to and including index three.
‫Basically this is the same as calling head but with four.
‫So car sales don't head for.
‫Excellent.
‫Okay.
‫Let's keep going.
‫Now, we've seen this previously, but let's say we wanted to select just this mate column and have
‫a look at what's going on there.
‫So the way to select a column is to type in its name in square brackets next to the name of the dataframe
‫as a string.
‫Shift into.
‫There's the mike column.
‫Let's have a look at the color column.
‫Carsales color.
‫Excellent.
‫We've selected that.
‫Now, there's two ways you might see.
‫I want you to be familiar with both of these ways of selecting a column.
‫So if we go to Carsales Dot make.
‫It's easier to type than something like that.
‫But this is the same thing.
‫Let me demonstrate.
‫Car sales make these two lines of code are going to do the exact same thing.
‫Let's prove it.
‫I'll put a split in here by hitting control.
‫Shift minus.
‫I'm going to go make.
‫Excellent.
‫And we're going to car sales.
‫Don't make.
‫Now, the only real difference between these two is the syntax.
‫So whichever one you prefer, you might want to use.
‫But what you should know about the dot notation is that if your column name has a space in it, the
‫dot notation won't work.
‫So if we tried to do this for odometer.
‫Kim.
‫It'll come back with an error.
‫But if we put a cell in here and go car sales.
‫Bio-dome came.
‫Run this.
‫It'll return the odometer.
‫So there there two main differences between selecting a single column.
‫And now one last thing before this video gets too long is that if you wanted to select a single column
‫but put a little bit of a filter on it, let's start with the mate column.
‫But we only want rows with the car.
‫Make Toyota.
‫So can you decipher what's going on here?
‫We're saying car sales.
‫Okay.
‫And then we're passing it.
‫This little condition here, this is called Boolean indexing.
‫Let's hit shift enter to have a look.
‫So this is going to say, hey, pandas, give us a car sales data frame.
‫And this condition is I only want the car sales data in the mate column, which are equal to Toyota.
‫Now, what if we only wanted maybe those with over 100,000 kilometres on the data, on the on the data,
‫on the odometer?
‫I've got data on the brain.
‫That's why we're saying it.
‫Let's get odometer here, Cam, because remember, we can't do the dot notation so you can create a
‫habit which one you want to type.
‫Usually you pick one way of selecting a single column and stick with that throughout your entire notebook.
‫So we want greater than 100,000.
‫Now.
‫This is going to give back the rose that fulfill this condition.
‫And so you can imagine how advanced you could get with this.
‫If you had, say, 100,000 different cars, you might put in a few more different criteria in here.
‫But this is just a few simple use cases of how we can view and select data.
‫Now there's a couple more we're going to go through, but to prevent this video from going for too long,
‫we're going to take a little break, go back over what we've gone just through and practice a little
‫bit before the next one.
‫But otherwise, we'll see some more ways of selecting and viewing data in the next video.