是的,函数被称为Index.get_indexer
并按名称列表返回列或索引的位置。
这样使用:
df = pd.DataFrame({
'a':[4,5,4,5,5,4],
'b':[7,8,9,4,2,3],
'c':[1,3,5,7,1,0],
'd':[5,3,6,9,2,4],
}, index=list('ABCDEF'))
print (df)
a b c d
A 4 7 1 5
B 5 8 3 3
C 4 9 5 6
D 5 4 7 9
E 5 2 1 2
F 4 3 0 4
cols = ['a','b','c']
df1 = df.iloc[1, df.columns.get_indexer(cols)]
print (df1)
a 5
b 8
c 3
Name: B, dtype: int64
df11 = df.iloc[[1], df.columns.get_indexer(cols)]
print (df11)
a b c
B 5 8 3
idx = ['A','C']
df2 = df.iloc[df.index.get_indexer(idx), 2:]
print (df2)
c d
A 1 5
C 5 6
我是一名优秀的程序员,十分优秀!