import pandas as pd
df = pd.DataFrame({'A':[1,2,3], 'B':[4,5,6]},
index=['first','second','third'])
从给定的行号列表中检索相应的索引很简单:
df.iloc[[1,2]].index
但是相反呢?假设我得到了一个列表['second','third']
。如何从中返回df
的行号0和1?
答案 0 :(得分:2)
尝试:
df.index.get_indexer(['second', 'third'])
输出:
array([1, 2], dtype=int64)
答案 1 :(得分:1)
我们也有<img src=[url] width=60% height=100%> /*relative::Let's say this set of images is in a
div who's css height is 300px. The images width given as 60% matches exactly its pixel
width, which is 180px.*/
get_indexer_for
对于获取值,只做索引切片就足够了,不需要过滤数据框
df.index.get_indexer_for(['second', 'third'])
Out[178]: array([1, 2], dtype=int32)