Get index and column of a specific value in a pandas dataframe

Question:

I have df. As an example, I can ask the following data.

data = {
   "calories": [420, 380, 390],
   "duration": [50, 40, 45]
}
#load data into a DataFrame object:
df = pd.DataFrame(data)

For example, there are 380 calories in 40 minutes. What I want to ask is:
How can I locate the number "40"? So how can I see the index and column name?

Input: "40"
Output: "1, duration"

enter image description here

Asked By: Omer Celikel

||

Answers:

This should work

# stack will pair the indices with column names
res = df.eq(40).replace(False, pd.NA).stack().index.values
res
# [(1, 'duration')]
Answered By: cottontail

You can use:

df.loc[df["duration"]==40].index

or

df.loc[ np.where(df==40)[1]]
Answered By: sushant patil