11

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?

3 Answers 3

12

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']
Sign up to request clarification or add additional context in comments.

4 Comments

If this column has a name to it, like 'First Pillar', how will the code be? I tried your code with name change to the df like new[new.Column == 17] but it returns error.
I think you need print (df[df['First Pillar'] == 17])
You can test it with df = pd.DataFrame({'First Pillar': {'Item 1': 0, 'Item 2': 20, 'Item 5': 12, 'Item 3': 34, 'Item 7': 17}}) print (df)
this does not work for floating point column values ://
2

use query

df.query('Column == 17')

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

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

Comments

2

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

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.