Pandas Finding Index From Values In Column
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']
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