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
>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