 Hello everybody! Today we're going to be looking at indexing in pandas. If you remember from previous videos the index is an object that stores the access labels for all pandas objects. The index in a data frame is extremely useful because it's customizable and you can also search and filter based off of that index. In this video we're going to talk all about indexing, how you can change the index and customize that, as well as how you can search and filter on that index and then we're also going to be looking at something a little bit more advanced called multi-indexing and you won't always use it but it's really good to know in case you come across a data frame that has that in it. So let's get started by importing pandas, import pandas as pd. Now we'll get our first data frame say df is equal to pd.read underscore csv and I've already copied this but we're going to do r and we're going to put this file path. So I have this world population csv, I will have that in the description just like I do in all of my other videos. Let's run df and let's take a look at this data frame. So we have a lot of information here. We have rank, country, continent, population, as well as the default index from zero all the way up to 233. Now if you haven't watched any of my previous videos on pandas the index is pretty important and it's basically just a number or a label for each row. It doesn't even necessarily have to be a unique number. You can create or add an index yourself if you want to and it doesn't have to be unique but it really should be unique especially if you want to use it appropriately. For what we're doing the country is actually going to be a pretty great index because the country you know is going to be all unique because we're looking at every single row as a different country as well as the population. So let's go ahead and create this country or add this country as our index. Now we can do this in a lot of different ways but the first way that you can do this if you already know what you are going to create that index on we can just go right in here when we're reading in this file and we'll say comma index underscore oops I spelled that completely wrong index underscore column and we'll say that is equal to and then we're going to say quote country so we're taking this country and we're going to assign it as the index. Now let's read this in and as you can see this is our index now it looks a little bit different we didn't have this country header right here which is specifying that this is still the country but you can tell that this is the index based off the bold letters as well as it being on the far left and all the regular columns for the data is over here while the country header is right here and it's lower than all the others just a quick way that you can see that that is the index. Now before we move on I want to show you some other ways that you can do this as well but I'm going to show you how to reverse this index before we move on and we'll say data frame so we had our data frame right here so we have data frame dot we'll say reset underscore index and then we'll say in place is equal to true which means we don't have to assign this to another variable and all that stuff it'll just be true so now when we run that data frame again the index was reset to the default numbers so now let's go down here I'll show you how to do this in a different way you can do df dot we'll say set underscore index and then we'll just say country so very similar to when we were reading in that file and we said set the index or that index column we said index column equals country if we do this and we run it in it works but if we say data frame right down here it's not going to save that if we want to save it just like we did above we're going to say in place is equal to true that is going to save it to where we don't have to assign another variable so now when we run this the data frame right here which is going to populate this the data frame is going to say in place is equal to true so that country will now be our index again let's run this and there we go really quickly I wanted to give a huge shout out to the sponsor of this entire panda series and that is you to me you to me has some of the best courses at the best prices and it is no exception when it comes to pandas courses if you want to master pandas this is the course that I would recommend it's going to teach you just about everything you need to know about pandas so huge shout out to you to me for sponsoring this panda series and let's get back to the video now what's really great about this index is we're able to search based off just this index and so we can filter on it and basically look through our data with it and there are two different ways that you can do that at least this is a very common way that people who use pandas will do to kind of search through that index the first one is called lock and there's lock and I lock and that stands for location or integer location let's look at lock first let's say df dot lock and then we'll do a bracket now we're able to specify the actual string the label so let's go right up here and let's say Albania so we'll say Albania so again this is just looking at the location let's run this now it's going to bring up all the Albania data just like here where it kind of looks like a column in a column and we can get this exact same data but using I lock right here and when we ran lock we're searching based off Albania which is in the zero one position so if we actually pull the one position for that integer the I lock we can look at the one position and this should give us the exact same data now let's take a look at multi indexing and we'll come back to a little bit of this in a second so multi indexing is creating multiple indexes we're not just going to create the country as the index now we're going to add an additional index on top of that so let's pull up our data frame right now we have the country but let's do dot reset index and we'll say in place equals true oops let's run it so now we have our data frame now let's set our index but this time when we set our index we're going to add the country as the index as well as the continent as an index so we'll say data frame dot set underscore index then we'll do a parentheses and instead of just doing country like we did before we're going to create a list oops and we'll do it like that and then we'll say oops continent and separated by a comma so we have continent and country let's just say in place is equal to true now when we run this we're going to have two indexes let's see what this looks like and let's run this so now we have country as well as continent as our index now you may notice that these indexes are repeating themselves on this continent index we have europe right here and europe right here as well as asia and asia and it looks a little bit funky but we are able to sort these values and make it look a lot better so let's go ahead and try this we'll do df dot sort underscore index and when we run this it should sort our index alphabetically and we can also look in here and see what kind of things we can you know specify we can specify the axis but it's automatically going to be looking at the zero this is zero and this is one so we have two axes within our data frame you choose the level whether it's ascending or not ascending in place kind string sort remaining all of these different things the only one that i really you know think is worth looking at is the ascending we already know some of these other ones but if we look at ascending let's run it now it's sorted these and so now it's kind of grouped together so we have africa and all the african ones as well as south america and all the south american ones let's really quickly say pd dot set underscore option and we'll say display dot max dot columns and just like this let's run it and i need to specify specify right here let's see how many rows we have 235 so let's do 235 let's run this and now when we run this you can see that africa is all grouped together and all the countries are in alphabetical order under it and then we go all the way down to asia and again just all in alphabetical order if we wanted to we could say ascending equals true and then when we run this oh i meant say false and then when we run this it's the exact opposite so it starts with south america the last one and then goes in reverse alphabetical order we could also say false make a list and do comma true and just like this and then it would sort this first column as false and this next column as true so you can really customize it but you know for what we're doing we don't need any of that we just need to be able to see this right here so now when we try to search by our index like we did before we did data frame dot look now when we did that and we said you know let's say angola when we specified angola it's not going to work properly because it's searching in this first index for the first string that we have we can search africa let's search for africa and now we have all of the african countries and if we want to specify to angola we can also go down another level oops by doing angle angola and now we have what we were looking at before where we're calling all the data within those but we couldn't do it just based off africa because we had an additional index right here so once we called both indexes now we get this view but let's look at that i look really quick when we run this let's just say one because right up here oh we have angola zero and then one so you think it may pull up angola let's go ahead and run this and it's still pulling up albania let's go right up here if you remember when we didn't have the multiple indexes it was pulling up albania the difference when you're doing these multi indexes is that the loke is able to specify this whereas this one does not go based off that multi indexing it's going to go based off the initial index or the integer based index so that's a lot about indexing in pandas we'll cover even a few more things in future videos as we get more and more into pandas but this is a lot of what indexing looks like within pandas and again super important to learn how to do and know how to do because it's a pretty important building block as we go through this pandas series so i hope you enjoyed this video on indexing if you did be sure to like and subscribe below and i will see you in the next video