Dorky
Dorky

Reputation: 121

Pandas Finding Index From Values In Column

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?

Upvotes: 11

Views: 16489

Answers (3)

NL23codes
NL23codes

Reputation: 1219

I tried one of the methods above and it did not work for me. I then put a bit more thought into it and realized I was making it more complicated than it needed to be. Here's the method I am using in my own program to get this functionality:

x = 17
df = pandas.DataFrame({'Item':[1,2,3,150],'Column':[0,20,34,17]})
response = df[df['Column'] == x].iloc[0]['Item']

print(response)

Output:

150

Upvotes: 2

jezrael
jezrael

Reputation: 863611

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

Upvotes: 12

piRSquared
piRSquared

Reputation: 294526

use query

df.query('Column == 17')

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

df.query('Column == 17').index.tolist()

Upvotes: 2

Related Questions