Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas Finding Index From Values In Column

Tags:

python

pandas

In using Python Pandas on a big dataset, how can I find the index based on the value in the column, in the same row?

For example, if I have this dataset...

           Column
Item 1     0
Item 2     20
Item 3     34
...
Item 1000  12

... and if I have this value 17 in one of the 1000 rows (excluding row 0) in the column, and I want to find out which one of the Item has this value 17 in the column in the same row, how can I do that?

For example, I want to find out what and where is this Item x indexed in the dataset as shown below...

           Column
Item x     17

... how can I do that with Pandas, using this value 17 as reference?

like image 725
Dorky Avatar asked Nov 18 '16 09:11

Dorky


People also ask

How do you find the index value of a DataFrame column?

To get the index value of any dataframe's column, the get loc() function can be used. To find the index, we merely supply the column label to the get_loc() function. Let's create a dataframe consisting of more than one column so we can retrieve its index location or index value.

How do you find the row index of an element in a DataFrame?

Use pandas.DataFrame. loc[] you can get rows by index names or labels. To select the rows, the syntax is df.

How do I index a column in Pandas?

In order to set index to column in pandas DataFrame use reset_index() method. By using this you can also set single, multiple indexes to a column. If you are not aware by default, pandas adds an index to each row of the pandas DataFrame.


2 Answers

Use boolean indexing:

df.index[df.Column == 17]

If need excluding row 0:

df1 = df.iloc[1:]
df1.index[df1.Column == 17]

Sample:

df = pd.DataFrame({'Column': {'Item 1': 0, 'Item 2': 20, 'Item 5': 12, 'Item 3': 34, 'Item 7': 17}})
print (df)
       Column
Item 1       0
Item 2      20
Item 3      34
Item 5      12
Item 7      17
print (df.index[df.Column == 17])
Index(['Item 7'], dtype='object')

print (df.index[df.Column == 17].tolist())
['Item 7']

df1 = df.iloc[1:]
print (df1)
        Column
Item 2      20
Item 3      34
Item 5      12
Item 7      17

print (df1.index[df1.Column == 17].tolist())
['Item 7']
like image 153
jezrael Avatar answered Sep 18 '22 15:09

jezrael


use query

df.query('Column == 17')

use index.tolist() to get the list of items

df.query('Column == 17').index.tolist()
like image 35
piRSquared Avatar answered Sep 22 '22 15:09

piRSquared