Welcome back.
Well we've had a look at a few different ways of describing data that we've imported depend is in into
a data frame.
Let's have a look at a couple of different ways we can view and select data.
So come down here we'll put it another little heading in.
So ago viewing and selecting data we're going to hit escape an 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.
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 10 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 quick changes here and there you might want to look at the top five rows and
a five isn't enough.
Maybe you want to look at the top seven or 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 data frame 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 our lock and I lock.
So let's write them down here.
Lock and Dot I lock now before we go into the details.
Remember where you always want to run code first.
If in doubt run the code.
That's our motto.
So let's create a series here so we can demonstrate the difference between lock and 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 0 2.
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
it in a list to say 0 3 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 don't lock three.
If we call this what do you think we'll come back and lock you can consider is short for location.
So let's try shift into dog and Snake.
Beautiful.
So what lock refers to is the index numbers.
So because we have three and three here it returns two items.
Let's try animals not lock nine.
What do you think this will come back with.
Which animal will shift into bird.
The reason being is because bird is at index nine so it created bird.
One two three one two three.
Index nine.
Okay now let's try our car sales data frame.
You're not sure what this looks like.
We'll run it here so we can see.
Okay.
0 1 2 3 the index is still in order here.
Beautiful.
Now let's run a lock on this dot lock.
3 What do you think this will come back with.
Which car let's try it out.
All beautiful comes back with position 3 a beautiful black BMW the twenty two thousand dollars can be
all yours I could become a car salesman What do you think so now let's try another one let's try.
I lock this time we'll put I lock here so got animals don't I lock three this time we're doing that
I lock instead of just lock so 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 0 1 2 3 if this was in order remember
Python lease and data frames in series start from zero I look refers to a position whereas the lock
refers to index Let's try maybe with our car sales data frame car sales to I lock three
it comes back with the same as lock 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 member because I like refers to position so
zero one two three is the same as index three.
So that's the main two points you have to remember about I 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.
OK.
Now the beautiful thing about this is that with I lock and lock you can use slicing.
So if you've ever used Python lists you might be familiar with slicing but if not I'll show you what
that looks like might type in animals that I lock.
Colon 3 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 3.
Now I lock doesn't include position 3.
So give us remember stuff from 0 0 1 2 3.
So it's going to give us up to 3.
Now let's try it with the car sales data from car sales.
Don't lock.
Colon three shift and enter.
There we go.
It's given us up to and including index 3.
Basically this is the same as calling head but with for 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 make 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 data
frame as a strength shift into there's the make column.
Let's have a look at the color column.
Car sales 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.
Car sales.
Dot make it easy as it's hype then 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 any I make.
Excellent.
We're going to a 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 K M it will come back with an error.
But if we put a cell in here and go car sales odometer K and run this it'll return the odometer.
So there are 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 make column.
But we only want rows with that car maker 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 entered to have a look.
So this is going to say hey panders give us a car sales data frame and this condition is I only want
the car sales data in the make column which are equal to Toyota.
Now what if we only wanted maybe those with over 100000 kilometers on the data on the the data on the
odometer.
We've got data on the brain as well 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 when 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 100000.
Now this is gonna give back the rows that fulfill this condition so you can imagine how advanced you
could get with this if you had say 100000 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.