Day-8 Unlock Your Data: Mastering Indexing and Selection

Akansha Yadav covers four pandas accessors for retrieving data from DataFrames: .loc for label-based indexing, .iloc for position-based, and .at and .iat for faster scalar access. She demonstrates each with an NBA dataset, showing how to extract rows, columns, and individual cells.
Chapters
Ladies and gentlemen, so hello everyone, welcome back to my YouTube video. In today's video, we will be talking about what is indexing and selection, because in the process of data analysis, these two concepts are very important— indexing and selection. So basically, we will first see what is the difference between the indexing and the selection. So what is indexing? Indexing is basically a process of identifying and accessing the specific element or row or column from the DataFrame or the series based on the labels or the integers that you have. So there are multiple operations using which you can do this. So one is loc, iloc, add, iadd, or simply a bracket notation also you can use to access the data based on the labels and the positions. Another method is the selection, which is the process where you are going to choose the specific subset on the basis of the criteria or the condition. There are multiple operations to do the same, just like Boolean indexing or selecting a subset by using the slicing. But in this video, we will be talking only about indexing. So as I mentioned that there are multiple operations using which you can do indexing, which is loci, loci_at, and at. So let's see what are those methods and what they, they do mean. So let's get started with loc method. So loc method is basically the label-based indexing method where you have the syntax df.loc, then row_label and then column_label. That is the syntax of loc method. Loc means you are going to enter the label to extract any row or the column. So that's the specific element that you wanted to extract, you have to mention the row label and then the column label, and that's it. In case of iloc, you are going to use the position-based indexing, which allows you to extract the data using integer index. The syntax to do that is df.iloc bracket row index and then the column index. So just like in the values, I have written df.iloc . Another method is at method, which is similar to loc but optimized for the scalar access by label. It is faster than loc method, but it is similar to that. And then you have— what you have to do is you have to write df.add bracket row label and then the column label. This is— this you can use to access individual element effectively by their labels. Same way we have iat method, which is similar to iloc method with— but optimized for scalar access by the integer position. In this case also, you have to write df.iat bracket row index and then the column index. So first is what you have to do is to implement all these methods. What I have done is I have imported a data which is NBA.csv data, which looks something like this, where we have name, team, numbers. This we have used in the earlier video as well. So using loc method, how to use the loc method? Suppose I wanted to access the first row. First row means which is at the zeroth with the label 0. So I have written df.iloc and 0. This way I can access the first row. If you want to another row, then you have to enter that particular label in this. Suppose I want only specifically the first name, so which is at the 0th column, and the column name is name. So we are writing the name labels are provided to that data. Same way I want, suppose first column entirely, I don't want any other things. So what I have done is I have written colon, it means from starting to end it will take. This is— we have not mentioned anything, so that means that it will take the entire data. And then this is the name variable, column name. So that's how we are getting the entire column. Another method is iloc method, which says this is the position-based indexing. df.iloc bracket 0, which will access the first row. Here we are getting the first row using iloc. In this case, this is index, this is not the label. In this next example that I have written over here is it's df.iloc . 0 is for label, uh, row, and this one is for column. And this is how we are getting the individual value using ILOC indexes. For ILOC, I am using— suppose I wanted to print the entire row. So now in ILO— according to ILOC, you cannot enter any label. It means column name is also the label, so you cannot enter that. You have to just work on the index. Axis. It means the integer values. So what I have done is I want entire data from starting to end, but so I have written no numbers. I have written— I have just written the colon, and then I have written comma, then proceeding it with the 0. 0 denotes that this is the— I am talking about the first column. So that's how we are getting the first column in the output. Then we have two more methods. One is the add method, which is label-based scalar axis, you can easily access the first element. So in according to that, what I have done is this is 0, which is for the row, and this is name, which is for the column, and this is the label-based indexing. It means this is similar to LOC. So I have written name, and that's how we are getting the first value. In another case, I'm right— I have written 0 comma 0. It means this is for the row, this is for the column, and I am not using any label. I am using integer values. I am using the numeric values, and I am getting the first value. So that's how you can do the indexing using pandas. So what I would suggest you to do is, I would suggest you that go on to the Kaggle website and download one dataset. Once you have downloaded that dataset, create a notebook and then upload that data just similar to this one. Once you have imported the data and in, uh, in the file, what you have to do is you have to try practicing all these indexes method. These are only 4 methods which will be very efficient for you. So I would recommend you to go and practice all these methods. So I hope you like this video. Don't forget to hit the like button, subscribe to the channel if you like it. Until the next video, bye-bye.










