繁体   English   中英

找到非数字索引值的数字位置

[英]locate the numeric position of a non numeric index value

考虑系列s如下:

s = pd.Series(np.arange(18, 0, -3), list('ABCDEF'))
s

A    18
B    15
C    12
D     9
E     6
F     3
dtype: int32

我想得到'D'的数字位置

这样做会,但我认为我们都同意这是严重的:

s.reset_index().index.to_series()[s.reset_index().iloc[:, 0] == 'D'].iloc[0]

您可以使用Index.get_loc

print(s.index.get_loc('D'))
3

使用np.searchsorted -

np.searchsorted(s.index.values,'D')

或者像这样使用方法 -

s.index.searchsorted('D')
m = s.index == 'D'
idx = m.argmax() if m.any() else None

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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