UNDERSTANDING LOC[ ] & iLOC[ ] IN PANDAS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome back to our channel so in today's session we'll discuss about the difference between lvoc and ilvoc in pandas so for that purpose we'll take the excel sheet so in this excel we are having some data the students marks and first of all we need to import the pandas and we have to uh read the data from the excel and create the data frame so first let us complete all those those things and then we need to use the lvoc and ilo so import pandas and i will give some alias name so yes pd and now i need to import the excel sheet so d is equal to pd dot read underscore excel and write down the location here so just right click on the file and go to the options there you can find the complete location of that particular file so just copy that one so this is a file location just copy this one and here you just copy slash grades dot x l s e x right so yeah capital letters grades dot x l s x and also use double slashes in the path right and now the complete data will be available in d so we need to you create the data frame so df is equal to pd dot data frame and give the d as a parameter right so now if you print the df will get the data which is available in excel sheet you can observe here right so now we will see what is the use of lvoc and iloc so actually lvoc is to retrieve the data in terms of rows and columns and that is completely a label-based data selecting method and whereas iloc is also used to retrieve the data based upon the rows and columns but here it is an index based data selecting method right so i will show you the difference so if you want to access the data if you want to get the data see this is the complete data so i will just so you can observe here so there are different data okay so one two three four five six seven and eight columns are there and around 20 rows are there so we need to get the data with the help of rows and columns for that we can use a df dot loc use the subscript and here you have to give the rows and columns so rows and columns you need to differentiate them by using the column okay comma so first of all if i want to get some three rows so i'll get the third row okay third row so using three means a third row okay and if you observe here three comma and here you need two columns and that should be in a label base you have to give the column name so if you want to sec if you want to get the second column if you give here second so you will not get an result so you'll get a key error so instead of giving the second you need to give the name of the column see i i will just give the name of the column here so name of a student i have to pass that in quotations and if i give so i'll get the name of that particular row see the third row the third row the name is some suresh so we'll get the data so that means instead of giving the numbers index we need to give the label so lvoc is completely based upon the label so that's why we call it as a label based data selecting methods so if you want to get a multiple numbers so use the slicing operation so if i go with a df dot loc 0 2 4 i will get the values from 0 to 4 all the rows 0 through to 4th row and similarly if i want to get the only the names give the name of student so that we'll get only the names only the names and if you want to get a multiple values you can simply go with comma and use the percentage percentage and use the both in a list because these two comes under a single element right these two comes under as a single element so this is row and these are the columns so that's why we have to enclose the multiple labels in square braces if you execute sorry just a second okay so i think percentage is in capital letters okay percentage is in capital letters we have given the small letters percentage so you can observe here so we got the values the rows from 0 to 4 and the name of the student and percentage the name of the student and percentage so if you want to get if you want to use the slicing you have to apply the slicing for labels itself so i will just copy here and you can observe here so name of the students and take the square braces and use colon and to which you need so i have to get the values till social okay till social so you just observe here so give the slicing operation social so you will get all the elements with the given slicing of rows and the slicing for columns so here we are representing the columns with the help of label with the help of a label okay if you copy the same thing and if you apply the iloc what happens we'll see the same thing we are applying but we are using the iloc so we'll get an error because here in column we have to represent in a indexes only indexes the column should be represented as indexes so ilc is completely a index based select data selecting method so instead of giving this one i will again give the slicing operation from 0 to or a 1 to some five so we'll get this one okay so i will go with the seven right so we'll get up to social right so this is how we can apply and one more thing you can observe if you are using iloc the last index value will not be considered the last index value will not be considered that's a very important point see for example df dot loc of some 0 to a 4 okay if you execute will get the rows from 0 1 2 3 and 4. so if you apply the same thing if you apply the same thing by using iloc you can observe we'll get only the values of rows 0 1 2 3 so because i if we are using the i l o c it will be considered as a indexes so if you are giving the index 4 the stop index it should not be included so that's why less than the stop index will be the last row okay and clearly we have to give the column also with the help of indexes with the help of indexes so obviously we'll get the complete thing if you go with the eight we'll get the percentage okay so eight that means the seventh index seven six five four three two one and zero okay and if you go with here only zero you'll get only roll numbers only roll numbers so if you want to display only the number roll number and the percentage you can go with df dot iloc and if you know the index then you can go with this one so zeroth index comma seventh index so automatically it will return the two rows okay two rows okay and here uh in all rows i have to take only two columns so if you are not giving anything so this will be considered as a rows so the rows only two rows will be displayed that means the zero throw and seventh row so if you want to give the column you have to give the column and comma so the first one represents the rows that means complete rows 20 rows and among these 20 rows only 0 and 7 columns will be displayed only 0 and 7 columns will be displayed hope you understood this one right so if you are using comma and specifying the index in sub list that implies only these two indexes values will be displayed and if you are using colon then in between all the values will be displayed so loc will give the complete value this means this will use the rows number row number so 0 to 4 so for example see this gives the roll numbers row numbers 0 to 4. so it will display all the rows that means with the 0 1 2 3 and 4 coming to the iloc here 0 and 4 will be the indexes so last index will not be considered so 0 1 2 will be 1 2 3 will be displayed right so hope you understood this one so this is a simple difference between loc and iloc so both the functions are used to retrieve the data from the data frame but loc will give the row numbers and the column numbers are called i mean column should be represented with the help of a label whereas in iloc the rows will be rows and columns will be represented in terms of index values okay so hope you understood this one so let's stop here and if you are having any doubts regarding this lvoc and ilvoc so feel free to post your doubts in the comment section definitely i will try to clarify all your doubts and if you really enjoyed my session like my session share my session with your friends and don't forget to subscribe to our channel thanks for watching thank you very much
Info
Channel: Sundeep Saradhi Kanthety
Views: 4,895
Rating: 4.9259257 out of 5
Keywords: sundeep, saradhi, kanthety, python, basics, fundamentals, programming concepts, object, programming fundamentals, PYTHON PROGRAMMING, PYTHON FUNDAMENTALS, interpreter, python libraries, libraries, numpy, pandas, numpy installation, python IDE, install and uninstall, PANDAS, idle, anaconda, series, dataframe, panel, csv filed, read_csv, read_excel, add column, drop column, manipulations, manipulating dataframe, loc in pandas, iloc in pandas, label based, index based, loc vs iloc, .loc, .iloc
Id: lJDtzZsmF0g
Channel Id: undefined
Length: 11min 13sec (673 seconds)
Published: Thu Jun 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.