- 7 hours ago
Make Pandas DataFrames easier to navigate by learning how to set, reset, sort, and search their indexes. This Python data analysis tutorial uses a world population dataset to show how a column such as country can become the index and how to filter rows with .loc and .iloc.
The walkthrough also introduces multi-indexing with country and continent, sorting index levels, and retrieving data from a hierarchical index. A practical introduction to Pandas indexing, DataFrame labels, row selection, and filtering for beginners learning Python data analysis or working with CSV datasets.
The walkthrough also introduces multi-indexing with country and continent, sorting index levels, and retrieving data from a hierarchical index. A practical introduction to Pandas indexing, DataFrame labels, row selection, and filtering for beginners learning Python data analysis or working with CSV datasets.
Category
📚
LearningTranscript
00:00Hello, everybody. Today we're going to be looking at indexing in Pandas. If you remember from previous videos, the index
00:05is an object that stores the access labels for all Pandas objects. The index in a data frame is extremely
00:11useful because it's customizable, and you can also search and filter based off of that index. In this video, we're
00:17going to talk all about indexing, how you can change the index and customize that, as well as how you
00:21can search and filter on that index. And then we're also going to be looking at something a little bit
00:25more advanced called multi-indexing.
00:28And you won't always use it, but it's really good to know in case you come across a data frame
00:33that has that in it. So let's get started by importing pandas. Import pandas as pd. Now we'll get our
00:40first data frame. We'll say df is equal to pd.read underscore csv. And I've already copied this, but we're
00:48going to do R. And we're going to put this file path.
00:52So I have this world population CSV. I will have that in the description, just like I do in all
00:57of my other videos. Let's run DF and let's take a look at this data frame. So we have a
01:03lot of information here. We have rank, country, continent, population, as well as the default index from zero all the
01:11way up to 233. Now, if you haven't watched any of my previous videos on pandas, the index is pretty
01:16important and it's basically just a number or a label for each row.
01:20It doesn't even necessarily have to be a unique number. You can create or add an index yourself if you
01:26want to, and it doesn't have to be unique, but it really should be unique, especially if you want to
01:31use it appropriately. For what we're doing, the country is actually going to be a pretty great index because the
01:37country you know is going to be all unique because we're looking at every single row as a different country
01:43as well as the population. So let's go ahead and create this country or add this country as our index.
01:49Now we can do this in a lot of different ways, but the first way that you can do this,
01:53if you already know what you are going to create that index on, we can just go right in here
01:58when we're reading in this file and we'll say comma index underscore, oops, I spelled that completely wrong, index underscore
02:05column. And we'll say that is equal to, and then we're going to say quote country. So we're taking this
02:13country and we're going to assign it as the index. Now let's read this in. And as you can see,
02:19This is our index. Now it looks a little bit different. We didn't have this country header right here, which
02:25is specifying that this is still the country. But you can tell that this is the index based off the
02:29bold letters, as well as it being on the far left. And all the regular columns for the data is
02:36over here, while the country header is right here, and it's lower than all the others. Just a quick way
02:41that you can see that that is the index. Now before we move on, I want to show you some
02:44other ways that you can do this as well. But I'm going to show you how to
02:48reverse this index before we move on and we'll say data frame so we had our data frame right here
02:54so we have data frame dot we'll say reset underscore index and then we'll say in place is equal to
03:02true which means we don't have to assign this to another variable and all that stuff it'll just be true
03:07so now when we run that data frame again the index was reset to the default numbers so now let's
03:13go down here and i'll show you how to do this in a different way
03:16We can do df. We'll say set underscore index. And then we'll just say country. So very similar to when
03:23we were reading in that file and we said set the index or that index column. We said index column
03:28equals country. If we do this and we run it in, it works. But if we say data frame right
03:34down here, it's not going to save that. If we want to save it, just like we did above, we're
03:40going to say in place is equal to true.
03:44That is going to save it to where we don't have to assign another variable. So now when we run
03:48this, the data frame right here, which is going to populate this, the data frame is going to say in
03:53place is equal to true. So that country will now be our index again. Let's run this. And there we
03:59go.
04:00Really quickly, I wanted to give a huge shout out to the sponsor of this entire Pandas series, and that
04:04is Udemy. Udemy has some of the best courses at the best prices, and it is no exception when it
04:08comes to Pandas courses. If you want to master Pandas, this is the course that I would recommend. It's going
04:13to teach you just about everything you need to know about Pandas. So huge shout out to Udemy for sponsoring
04:17this Pandas series, and let's get back to the video. Now, what's really great about this index is we're able
04:22to search based off just this index. And so we can filter on it and basically look through our data
04:27with it.
04:28And there are two different ways that you can do that. At least this is a very common way that
04:32people who use pandas will do to kind of search through that index. The first one is called lock, and
04:37there's lock and ilock. And that stands for location or integer location. Let's look at lock first. Let's say df
04:44.lock, and then we'll do a bracket. Now we're able to specify the actual string, the label. So let's go
04:51right up here, and let's say Albania. So we'll say Albania.
04:56So again, this is just looking at the location. Let's run this. Now it's going to bring up all the
05:01Albania data, just like here, where it's kind of looks like a column in a column. And we can get
05:07this exact same data, but using ilock right here. And when we ran lock, we were searching based off Albania,
05:16which is in the zero one position. So if we actually pull the one position for that integer, the ilock,
05:24we can look at the
05:26one position, and this should give us the exact same data. Now let's take a look at multi-indexing, and
05:33we'll come back to a little bit of this in a second. So multi-indexing is creating multiple indexes. We're
05:39not just going to create the country as the index. Now we're going to add an additional index on top
05:44of that. So let's pull up our data frame. Right now we have the country, but let's do dot reset
05:50index, and we'll say in place
05:55equals true oops let's run it so now we have our data frame now let's set our index but this
06:03time when we set our index we're going to add the country as the index as well as the continent
06:08as an index so we'll say data frame dot set underscore index then we'll do a parentheses and instead of
06:16just doing country like we did before we're going to create a list oops and we'll do it like that
06:22and then we'll say
06:25oops, continent, and separate it by a comma. So we have continent and country. Let's just say in place is
06:35equal to true. Now, when we run this, we're going to have two indexes. Let's see what this looks like.
06:43Let's run this. So now we have country as well as continent as our index. Now, you may notice that
06:52these indexes are repeating themselves on this continent index. We have
06:56Europe right here and Europe right here, as well as Asia and Asia. And it looks a little bit funky,
07:03but we are able to sort these values and make it look a lot better. So let's go ahead and
07:08try this. We'll do df.sort underscore index. And when we run this, it should sort our index alphabetically. And
07:17we can also look in here and see what kind of things we can specify.
07:22We can specify the axis, but it's automatically going to be looking at the zero. This is zero and this
07:27is one. So we have two axes within our data frame. You choose the level, whether it's ascending or not
07:33ascending, in place, kind, string, sort remaining, all of these different things. The only one that I really, you know,
07:41think is worth looking at is the ascending. We already know some of these other ones. But if we look
07:45at ascending, let's run it.
07:47Now it's sorted these. And so now it's kind of grouped together. So we have Africa and all the African
07:53ones, as well as South America and all the South American ones. Let's really quickly say PD dot set underscore
08:02option. And we'll say display dot max dot columns. And just like this, let's run it.
08:12And I need to specify, specify right here. Let's see how many rows we have. 235. So let's do 235.
08:22Let's run this. And now when we run this, you can see that Africa is all grouped together and all
08:29the countries are in alphabetical order under it. And then we go all the way down to Asia. And again,
08:34just all in alphabetical order. If we wanted to, we could say ascending equals true.
08:42And then when we run this, oh, let's say false. And then when we run this, it's the exact opposite.
08:48So it starts with South America, the last one, and then goes in reverse alphabetical order. We could also say
08:53false, make a list and do comma true. And just like this, and then it would sort this first column
09:02as false and this next column as true. So you can really customize it, but for what we're doing, we
09:08don't need any of that. We just need to be able to see this right here.
09:11so now when we try to search by our index like we did before we did dataframe.loc now when
09:18we did that and we said you know let's say angola when we specified angola it's not going to work
09:24properly because it's searching in this first index for the first string that we have we can search africa let's
09:34search for africa and now we have all the african countries and if we want to specify
09:41to angola we can also go down another level oops by doing angle angola and now we have what we
09:49were looking at before where we're calling all the data within those but we couldn't do it just based off
09:54africa because we had an additional index right here so once we called both indexes now we get this view
10:00but let's look at that i look really quick when we run this let's just say one because right up
10:10here oh
10:12we have angola zero and then one so you think it may pull up angola let's go ahead and run
10:19this and it's still pulling up albania let's go right up here if you remember when we didn't have the
10:26multiple indexes it was pulling up albania the difference when you're doing these multi-indexes is that the loc is
10:34able to specify this whereas
10:37This one does not go based off that multi-indexing. It's going to go based off the initial index or
10:42the integer-based index. So that's a lot about indexing in pandas. We'll cover even a few more things in
10:49future videos as we get more and more into pandas. But this is a lot of what indexing looks like
10:55within pandas. And again, super important to learn how to do and know how to do because it's a pretty
10:59important building block as we go through this pandas series.
11:03So I hope you enjoyed this video on indexing. If you did, be sure to like and subscribe below, and
11:08I will see you in the next video.