Location of specific value in df. How can I locate the number "40"?

Viewed 43

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

2 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')]

You can use:

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

or

df.loc[ np.where(df==40)[1]]
Related