简体   繁体   中英

Index find position for matching string

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

Try this:

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

我建议您输入:

index_id.get_loc("a")

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

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

Output:

[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM