49

If we have a known value in a column, how can we get its index-value? For example:

In [148]: a = pd.DataFrame(np.arange(10).reshape(5,2),columns=['c1','c2']) In [149]: a Out[149]: c1 c2 0 0 1 1 2 3 2 4 5 ........ 

As we know, we can get a value by the index corresponding to it, like this.

In [151]: a.ix[0,1] In [152]: a.c2[0] In [154]: a.c2.ix[0] <-- use index Out[151]: 1 Out[152]: 1 Out[154]: 1 <-- get value 

But how to get the index by value?

5 Answers 5

54

There might be more than one index map to your value, it make more sense to return a list:

In [48]: a Out[48]: c1 c2 0 0 1 1 2 3 2 4 5 3 6 7 4 8 9 In [49]: a.c1[a.c1 == 8].index.tolist() Out[49]: [4] 
Sign up to request clarification or add additional context in comments.

5 Comments

An index can have non-unique entries, why do you say it makes more sense to return a list?
Hmm, I think it's my wrong. If all the indices are unique, we can get the single index by a.c1[a.c1 == 8].index.tolist()[0]
thanks for you answer , it's a good ideal . I have not considered index object can be converted to plain list .thanks agagin.
@waitingkuo is their any sophisticated way released in 2017 to get the index?
@waitingkuo how can we do this if we want to insert two conditions? like a.c1[a.c1 == 8 <AND a.c2==10>]. doesnt work with this example but imagine if a.c2 == 10 also existed
39

Using the .loc[] accessor:

In [25]: a.loc[a['c1'] == 8].index[0] Out[25]: 4 

Can also use the get_loc() by setting 'c1' as the index. This will not change the original dataframe.

In [17]: a.set_index('c1').index.get_loc(8) Out[17]: 4 

Comments

8

The other way around using numpy.where() :

import numpy as np import pandas as pd In [800]: df = pd.DataFrame(np.arange(10).reshape(5,2),columns=['c1','c2']) In [801]: df Out[801]: c1 c2 0 0 1 1 2 3 2 4 5 3 6 7 4 8 9 In [802]: np.where(df["c1"]==6) Out[802]: (array([3]),) In [803]: indices = list(np.where(df["c1"]==6)[0]) In [804]: df.iloc[indices] Out[804]: c1 c2 3 6 7 In [805]: df.iloc[indices].index Out[805]: Int64Index([3], dtype='int64') In [806]: df.iloc[indices].index.tolist() Out[806]: [3] 

Comments

7

To get the index by value, simply add .index[0] to the end of a query. This will return the index of the first row of the result...

So, applied to your dataframe:

In [1]: a[a['c2'] == 1].index[0] In [2]: a[a['c1'] > 7].index[0] Out[1]: 0 Out[2]: 4 

Where the query returns more than one row, the additional index results can be accessed by specifying the desired index, e.g. .index[n]

In [3]: a[a['c2'] >= 7].index[1] In [4]: a[(a['c2'] > 1) & (a['c1'] < 8)].index[2] Out[3]: 4 Out[4]: 3 

Comments

2

I think this may help you , both index and columns of the values.

value you are looking for is not duplicated:

poz=matrix[matrix==minv].dropna(axis=1,how='all').dropna(how='all') value=poz.iloc[0,0] index=poz.index.item() column=poz.columns.item() 

you can get its index and column

duplicated:

matrix=pd.DataFrame([[1,1],[1,np.NAN]],index=['q','g'],columns=['f','h']) matrix Out[83]: f h q 1 1.0 g 1 NaN poz=matrix[matrix==minv].dropna(axis=1,how='all').dropna(how='all') index=poz.stack().index.tolist() index Out[87]: [('q', 'f'), ('q', 'h'), ('g', 'f')] 

you will get a list

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.