Hello everyone and welcome to the data frames per 3 lecture.
In this final lecture that data frame's series of lectures we're going to learn about multa index and
index higher key.
Well first create a quick example of what a multi indexed data looks like and then how you can work
with data frames to have a multilevel index.
Let's go ahead and jump to Jupiter notebook to get started.
All right.
Here I am at the Jupiter notebook.
What I'm going to go ahead and do is copy and paste some lines of code that are available for you in
your notebook called Data frames.
If you scroll down until you get to the multi Index index higher key section you'll see these four lines
of code.
These are the four lines of code are going to use to actually build this data frame.
We can go ahead and check this out.
We'll say outside and that's just the list.
So we have three or six items in this list of two different types G1 G1 G1 and then G-2 DTG 2 we have
inside which is 1 2 3 1 2 3.
And then when we went ahead and did was using the zip function along with the list function as we said
higher index we went ahead and used zip and LIST on this in order to make this into a list of tuple
pairs G 1 1 1 2 1 3 2 1 2 2 2 3.
And then I passed it into a special function from panderers and we were actually won't be working with
this function at all throughout the course but this is just for a customization of making a data frame
so don't worry too much about it but I just want you to be aware of it.
Its PD that multi-band index from tuples and it can basically take in a list that looks like this and
create a multi index from it.
So after you get done with that youll get this multi index with several levels.
Do you want me to add one level one to three as another level and then labels and I will go and explain
what all that means as we create this data frame.
But for now you can just feel free to copy and paste this code directly from the notebook since its
not important as far as creating that data frame from scratch.
We'll go in and say PD data frame passen Rande and 6 Com 2 so we have 6 rows by two columns and then
I'm going to set the index to be the higher index object we just made and then I will set the columns
to be a list a and b.
All right.
And you can feel free to copy and paste all this code from the notebooks because it's not that important
.
This is just for actually constructing a multi index level data frame.
Let me show you what I mean by that here.
If I actually show the data frame I can see that it has two levels of an index.
It has this Jiwon and G-2 level.
And then this sub or insight level one two three one two three and then two columns.
And this is a data frame looks like it has multi-levels of an index.
Otherwise known as an index higher key.
Let's go ahead and show how we can actually call data from a multilevel index for this index hierarchy
.
We can go ahead and do is say DMF L-O see.
And then we can go ahead and using Bracton notation GRABBE from the very outside index.
For instance if I want everything that's under G 1 I can just passen she one and I get that little sub
data frame in return which is basically the first three rows for those two columns and then I can continue
indexing off of this such as I'll see or ellos see either one is fine.
And then passing in number and that will actually then return these two items here as a series.
So the basic idea is you call from the outside index continue calling inside deeper.
If you actually want to name these index names.
So right now they are unnamed.
That's notified about the fact that there's no name in these two cells which can go out into say DMF
index.
Names do shift enter and you should get a frozen list that's none none.
And this is just a panderers index names object types.
All it's saying here is that these indices don't have any names but you can go ahead and do say equals
and then pass in a list of names.
For instance let's go out and call this outside index groups because it's first G.
And then we'll call the insight index and you m for numbers.
And now when I call my data frame we have groups as a labeled index on the outside and numb as a label
index on the inside.
And this could have as many layers as you want.
Hopefully you can begin to see how this would be useful for multilevel index information that you want
to use along of a data frame.
Let's go ahead and show you how we can grab information off of this one more time.
Imagine you want to grab this value right here.
0.07 to the one to have highlighted.
Well that's under G-2 number two column B.
Let's start from the outside and work our way in.
We'll go ahead and specify that we wants C G to and that returns this data frame.
Again we're looking for this value here.
So let's go ahead and do one more index and we can go ahead and call here too.
And then we have under column B or index B.
Excuse me for the series 0.07.
So then finally we just call B at the end and that's all we can grab all the way from the outside the
inside this particular value.
What you should do now is pause the video.
Select another value and see if you can successfully index it and return it back.
OK.
Hopefully you're able to do that.
Let's go ahead and show you one last very useful function called cross section and it's going to be
x x.
So this returns a cross section of rows or columns from a series of data frame and you're going to use
this when you have a multilevel index.
So again our data frame looks like this.
Let's say we wanted to grab everything that was under G 1 I could do what I just showed you EHLO C G
1 and that returns the data from energy 1.
But another option for a multilevel index is using this X as the cross section and then passing in Jiwon
also has a string.
But what's nice about this cross section is it has the ability to skip or go inside a multilevel index
.
For example let's say I have this data frame here and I want to grab all values of number equal to one
with both groups in it.
Meaning I want what I highlighted here 1 and then 0.3 1.6 9.
But also I wanted this value 0.1 6 0.1 8.
So I want all values were numb that insight index is equal to 1.
This sort of indexing is tricky for something like EHLO see methods but it's not so bad with the X s
or cross section method which you end up doing is specifying what you want as far as not equal to one
.
And then you have to indicate as a second arguments level and then pass in the name of the index.
In this case the name of the index is numb.
So essentially this allows us to grab a cross section where level equals numb is equal to 1 and that's
how you're mainly going to be using this cross section.
And now we see both groups where level num happens to be equal to 1.
And this sort of data gets returned.
OK don't worry too much since this is pretty advanced usage of Pandurs.
I just want you to be aware of it later on as we learn more about data visualization and you start reaching
your data.
Capstone projects we'll revisit this cross section method to actually grab stock index data.
But for now I want you to be aware of it but don't worry as far as being tested on it because it's more
advanced usage and you won't see this sort of stuff very often throughout the course.
In fact it really only shows up once in one of the more advanced data capstone projects and there's
an explanation of how to use cross section again.
So we'll go in and give you a refresher before you see this topic again.
For now this was more just an advanced overview of multi index topics and index higher key topics.
Mainly what you're going to want to do is index from the outside in step by step to get the data you
want out.
OK thanks everyone and I'll see you at the next lecture.