{"id":4491,"date":"2021-05-13T16:22:28","date_gmt":"2021-05-13T10:52:28","guid":{"rendered":"https:\/\/python-programs.com\/?p=4491"},"modified":"2021-11-22T18:42:41","modified_gmt":"2021-11-22T13:12:41","slug":"python-find-indexes-of-an-element-in-pandas-dataframe","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-find-indexes-of-an-element-in-pandas-dataframe\/","title":{"rendered":"Python: Find indexes of an element in pandas dataframe | Python Pandas Index.get_loc()"},"content":{"rendered":"
In this tutorial, we will learn how to find the indexes of a row and column numbers using pandas in a dataframe. By learning from this tutorial, you can easily get a good grip on how to get row names in Pandas dataframe. Also, there is a possibility to learn about the Python Pandas Index.get_loc() function along with syntax, parameters, and a sample example program.<\/p>\n
Pandas loc : int if unique index, slice if monotonic index, else mask<\/p>\n Also View:<\/span><\/p>\n The initial step is creating a dataframe.<\/p>\n Code:<\/p>\n Output:<\/p>\n <\/p>\n Now, we want to find the location where the value ’81’ exists.<\/p>\n We can see that value ’81’ exists at two different places in the data frame.<\/p>\n Now, we will proceed to get the result of this.<\/p>\n Dataframe object and the value as an argument is accepted by the function we have created.<\/p>\n It returns the list of index positions at all occurrences.<\/p>\n Code:<\/p>\n Output:<\/p>\n <\/p>\n We got the exact row and column names of all the locations where the value ’81’ exists.<\/p>\n We will see what happened inside the getIndexes<\/em> function.<\/p>\n Now, we will learn step by step process on what happened inside the getIndexes()<\/em> function.<\/p>\n Step 1: Get bool dataframe with True at positions where the value is 81 in the dataframe using pandas.DataFrame.isin()<\/strong><\/p>\n This isin() function accepts a value and returns a bool dataframe. The original size and the bool data frame size is the same. When the given value exists, it contains True otherwise False.<\/p>\n We will see the bool dataframe where the value is ’81’.<\/p>\n Output:<\/p>\n <\/p>\n It is of the same size as empDfObj. As 81 exists at 2 places inside the dataframe, so this bool dataframe contains\u00a0True<\/em> at only those two places. In all other places, it contains False<\/em>.<\/p>\n Step 2: Get the list of columns that contains the value<\/strong><\/p>\n We will get the name of the columns that contain the value ’81’.We will achieve this by fetching names in a column in the bool dataframe which contains True value.<\/p>\n Code:<\/p>\n Output:<\/p>\n <\/p>\n Step 3: Iterate over selected columns and fetch the indexes of the rows which contains the value<\/strong><\/p>\n We will iterate over each selected column and for each column, we will find the row which contains the True value.<\/p>\n Now these combinations of column names and row indexes where True exists are the index positions of 81 in the dataframe i.e.<\/p>\n Code:<\/p>\n Output:<\/p>\n <\/p>\n Now it is clear that this is the way the getIndexes() function was working and finding the exact index positions of the given value & store each position as (row, column) tuple. In the end, it returns a list of tuples representing its index positions in the dataframe.<\/p>\n Suppose we have multiple elements,<\/p>\n Now we want to find index positions of all these elements in our dataframe empDfObj, like this,<\/p>\n Let\u2019s use the\u00a0getIndexes()<\/em>\u00a0and\u00a0dictionary comprehension<\/em>\u00a0to find the indexes of all occurrences of multiple elements in the dataframe\u00a0empDfObj.<\/em><\/p>\n Output: dictOfPos<\/em>\u00a0is a dictionary of elements and their index positions in the dataframe. As \u2018abc<\/em>\u2018 doesn\u2019t exist in the dataframe, therefore, its list is empty in dictionary\u00a0dictOfPos<\/em>.<\/p>\n Hope this article was understandable and easy for you!<\/p>\n Want to expert in the python programming language? Exploring\u00a0Python Data Analysis using Pandas<\/a>\u00a0tutorial changes your knowledge from basic to advance level in python concepts.<\/p>\n Read more Articles on Python Data Analysis Using Padas \u2013 Find Elements in a Dataframe<\/strong><\/p>\n In this tutorial, we will learn how to find the indexes of a row and column numbers using pandas in a dataframe. By learning from this tutorial, you can easily get a good grip on how to get row names in Pandas dataframe. Also, there is a possibility to learn about the Python Pandas Index.get_loc() …<\/p>\nIndex.get_loc()<\/code>function results integer location, slice, or boolean mask for the requested label. The function acts with both sorted as well as unsorted Indexes. It gives various options if the passed value is not present in the Index.<\/p>\n
<\/a>Syntax:<\/h3>\n
Index.get_loc(key, method=None, tolerance=None)<\/pre>\n
<\/a>Parameters:<\/h3>\n
\n
<\/a>Return Value:<\/h3>\n
<\/a>Example using Index.get_loc() function:<\/h3>\n
# importing pandas as pd\r\nimport pandas as pd\r\n \r\n# Creating the Index\r\nidx = pd.Index(['Labrador', 'Beagle', 'Labrador',\r\n 'Lhasa', 'Husky', 'Beagle'])\r\n \r\n# Print the Index\r\nidx<\/pre>\n
\n
<\/a>Creating a Dataframe in Python<\/h2>\n
# List of Tuples\r\nempoyees = [('jack', 34, 'Sydney', 155),\r\n ('Riti', 31, 'Delhi', 177),\r\n ('Aadi', 16, 'Mumbai', 81),\r\n ('Mohit', 31, 'Delhi', 167),\r\n ('Veena', 81, 'Delhi', 144),\r\n ('Shaunak', 35, 'Mumbai', 135),\r\n ('Shaun', 35, 'Colombo', 111)\r\n ]\r\n# Create a DataFrame object\r\nempDfObj = pd.DataFrame(empoyees, columns=['Name', 'Age', 'City', 'Marks'])\r\nprint(empDfObj)<\/pre>\n
(4, 'Age')\r\n(2, 'Marks')<\/pre>\n
\n
<\/a>Find all indexes of an item in pandas dataframe<\/h2>\n
def getIndexes(dfObj, value):\r\n ''' Get index positions of value in dataframe i.e. dfObj.'''\r\n listOfPos = list()\r\n # Get bool dataframe with True at positions where the given value exists\r\n result = dfObj.isin([value])\r\n # Get list of columns that contains the value\r\n seriesObj = result.any()\r\n columnNames = list(seriesObj[seriesObj == True].index)\r\n # Iterate over list of columns and fetch the rows indexes where value exists\r\n for col in columnNames:\r\n rows = list(result[col][result[col] == True].index)\r\n for row in rows:\r\n listOfPos.append((row, col))\r\n # Return a list of tuples indicating the positions of value in the dataframe\r\n return listOfPos<\/pre>\n
<\/a>How did it work?<\/h3>\n
DataFrame.isin(self, values)<\/code><\/p>\n
# Get bool dataframe with True at positions where value is 81\r\nresult = empDfObj.isin([81])\r\nprint('Bool Dataframe representing existence of value 81 as True')\r\nprint(result)<\/pre>\n
# Get list of columns that contains the value i.e. 81\r\nseriesObj = result.any()\r\ncolumnNames = list(seriesObj[seriesObj == True].index)\r\n\r\nprint('Names of columns which contains 81:', columnNames)<\/pre>\n
# Iterate over each column and fetch the rows number where\r\nfor col in columnNames:\r\n rows = list(result[col][result[col] == True].index)\r\n for row in rows:\r\n print('Index : ', row, ' Col : ', col)<\/pre>\n
<\/a>Find index positions of multiple elements in the DataFrame<\/h2>\n
[81, 'Delhi', 'abc']<\/code><\/p>\n
81 : [(4, 'Age'), (2, 'Marks')]\r\nDelhi : [(1, 'City'), (3, 'City'), (4, 'City')]\r\nabc : []\r\n<\/pre>\n
listOfElems = [81, 'Delhi', 'abc']\r\n# Use dict comprhension to club index positions of multiple elements in dataframe\r\ndictOfPos = {elem: getIndexes(empDfObj, elem) for elem in listOfElems}\r\nprint('Position of given elements in Dataframe are : ')\r\nfor key, value in dictOfPos.items():\r\n print(key, ' : ', value)\r\n<\/pre>\n
\n<\/p>\n\n