Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find the exact location of an element in pandas dataframe

I have the following pandas dataframe:

df = pd.DataFrame({"A": [1,2,3], "B": [-2,8,1], "C": [-451,23,326]})

Is there any function that returns the exact location of an element? Assuming that the element exists in the table and no duplicate. E.g. if element = 326 then it will return row:2 col:2.
Many thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use np.where combined with df.values:

element = 326

indices = np.where(df.values == element)

row_index, col_index = indices[0][0], indices[1][0]

print("Row:", row_index)
print("Column:", col_index)

Output:

Row: 2
Column: 2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading