Although it seems very detrimental, I am having a hard time getting the index of a dataframe for a certain string or value at a random position in the dataframe.
I made an example dataframe:
fruits = {
'column1':["Apples","Pears","Bananas","Oranges","Strawberries"],
'column2':[1,2,3,4,5],
'column3':["Kiwis","Mangos","Pineapples","Grapes","Melons"]
}
df = pd.DataFrame(fruits)
column1 column2 column3
0 Apples 1 Kiwis
1 Pears 2 Mangos
2 Bananas 3 Pineapples
3 Oranges 4 Grapes
4 Strawberries 5 Melons
Now I want to get the position index of Mangos, without having the knowledge in which column or row it exists. So far I succeeded in getting the row index:
print(df.loc[df.isin(["Mangos"]).any(axis=1)].index)
Which results in:
Int64Index([1], dtype='int64')
But now I would also like to retrieve the column index or column name.
This thread is a very simplified version of Get column name where value is something in pandas dataframe, but I could not figure out the code using the other thread.