0

Consider following example

index_abcd = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'] data = ['1', '2', '3', '4', '5', '6', '7', '8', '9'] df = pd.DataFrame(data, index=index_abcd) index_id= df.index 

I want to find the position of 'a' in the index "index_id". How do I do that. Trying

index_id.index('a') 

does not work (error 'Index' object has no attribute 'index'). Thanks a lot

2
  • You could try enumerate(), like [i for i, x in enumerate(index_id) if x == 'a']. But this approach doesn't seem to be optimal if the list gets too big Commented Dec 8, 2017 at 9:12
  • 1
    you use a class Index, it's no longer a string, there is no index attribute. You should have a look to the class to see if there is another attribute you could use. maybe 'data'. You may try index_id.data.index('a') as data is a 1 dimension array Commented Dec 8, 2017 at 9:13

3 Answers 3

1

I suggest you to type :

index_id.get_loc("a") 
Sign up to request clarification or add additional context in comments.

2 Comments

Same here: Thanks, works. Unfortunately I can only give one check for good answer. (above)
Mine is one line code :), I'm kidding, most important is that you get the answer you want !
1

Try this:

index = pd.Index(list(df)) print index.get_loc('a') 

Comments

1

If I understand you correctly, this may be what you want:

print(df.ix['a'].index.tolist()) 

Output:

[0] 

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.