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

Time:06-25

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

CodePudding user response:

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

CodePudding user response:

You can use:

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

or

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