How to locate the position of cells in a Pandas Dataframe based on a specific condition using Python?

I have a dataset that has mostly numeric columns and I want to extract the position of a particular cell using some condition or criteria i.e., I want that cell’s row and column number. For example, if I have the following dataset:

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], \
                   'C': [7, 8, 9], 'D': [10, 11, 12]})

And if I want to extract the position of the cell with the value 6, then my desired output would be:

Row: 2
Column: 1

Please provide me with methods that can help me in this, example codes with each method will help me understand the method even more.

Hey @mubashir_rizvi, try using this “np.where()” method. The np.where() is a NumPy function that returns the indices of elements in an array that satisfy a given condition. When a single argument is provided, it returns the indices where the given condition is True. In this technique, np.where() is used to get indices (row and column number) of value 6.

Note: The indices would appear as one less than the sequential count of indexes, this is because the index starts from position 0 in Python.

Hello @mubashir_rizvi , you can do this by using loc indexer and get_loc() method. Let me explain it to you.