Python | Pandas Index.get_loc() Last Updated : 17 Dec, 2018 Comments Improve Suggest changes Like Article Like Report Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.get_loc() function return integer location, slice or boolean mask for requested label. The function works with both sorted as well as unsorted Indexes. It provides various options if the passed value is not present in the Index. you may choose to return the previous value or the next value to the passed value only if the Index labels are sorted. Syntax: Index.get_loc(key, method=None, tolerance=None) Parameters: key : label method : {None, ‘pad’/’ffill’, ‘backfill’/’bfill’, ‘nearest’}, optional -> default: exact matches only. -> pad / ffill: find the PREVIOUS index value if no exact match. -> backfill / bfill: use NEXT index value if no exact match -> nearest: use the NEAREST index value if no exact match. Tied distances are broken by preferring the larger index value. Returns : loc : int if unique index, slice if monotonic index, else mask Example #1: Use Index.get_loc() function to find the location of the passed value. Python3 # importing pandas as pd import pandas as pd # Creating the Index idx = pd.Index(['Labrador', 'Beagle', 'Labrador', 'Lhasa', 'Husky', 'Beagle']) # Print the Index idx Output : let's find out the location of 'Lhasa' in the Index. Python3 1== # Print the location of the passed value.. idx.get_loc('Lhasa) Output : As we can see in the output, the Index.get_loc() function has returned 3 indicating that the passed value is present at this location in the Index. Example #2: Use Index.get_loc() function to find the location of the passed value. If the passed value is not present in the Index then return the location of previous value that is just smaller than the passed value. Python3 # importing pandas as pd import pandas as pd # Creating the Index idx = pd.Index([1, 2, 3, 14, 25, 37, 48, 69, 100]) # Print the Index idx Output : Let's find the position of the value 33 in the Index. Python3 1== # Find the position of 33 in the index. # If it is not present then we forward # fill and return the position of previous value. idx.get_loc(33, method ='ffill') Output : As we can see the function has returned the output 3. As 33 is not present in the Index but in the sorted order the value that is just smaller than 33 is 25 and its location is 4. So, the output is 4. we do not set method parameter then it will result into error if the value is not present. Comment More infoAdvertise with us Next Article Python | Pandas Index.get_loc() S Shubham__Ranjan Follow Improve Article Tags : Technical Scripter Python Python-pandas Python pandas-indexing Practice Tags : python Similar Reads Python | Pandas Index.delete() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.delete() function returns a new object with the passed locations deleted. 2 min read Python | Pandas Index.data Pandas Index is an immutable ndarray implementing an ordered, sliceable set. It is the basic object which stores the axis labels for all pandas objects. Pandas Index.data attribute return the data pointer of the underlying data of the given Index object. Syntax: Index.data Parameter : None Returns : 2 min read Python | Pandas Index.get_values() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.get_values() function returns the Index data as an numpy.ndarray. It retu 2 min read Python | Pandas Index.slice_locs() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.slice_locs() function compute slice locations for input labels. It takes 2 min read Python | Pandas Index.flags Pandas Index is an immutable ndarray implementing an ordered, sliceable set. It is the basic object which stores the axis labels for all pandas objects. Pandas Index.flags attribute return the status of all the flags for the given Index object. Syntax: Index.flags Parameter : None Returns : status o 2 min read Python | Pandas Index.dtype Pandas Index is an immutable ndarray implementing an ordered, sliceable set. It is the basic object which stores the axis labels for all pandas objects. Pandas Index.dtype attribute return the data type (dtype) of the underlying data of the given Index object. Syntax: Index.dtype Parameter : None Re 2 min read Python | Pandas Index.all() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.all() function checks if all the elements in the index are true or not. I 2 min read Python | Pandas Index.min() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.min() function returns the minimum value of the Index. The function works 2 min read Python | Pandas Index.max() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.max() function returns the maximum value of the Index. The function works 2 min read Python | Pandas Index.any() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.any() function checks if any of the elements in the index are true or not 2 min read Like