如何获得已过滤的熊猫系列的索引?

问题描述 投票:0回答:1

我正在使用13548个连续值,我的数据是通过以下方式构造的:

s = pd.Series(delta, index=st)s[:10]

GEOM_190_190 0.00GEOM_190_192 1.91GEOM_190_194 2.54GEOM_190_196 4.90GEOM_190_198 6.03GEOM_190_200 6.92GEOM_190_202 8.60GEOM_190_204 10.06GEOM_190_206 11.34GEOM_190_208 12.43dtype:float64

我应用了以下过滤器:extra = [i for i in s if i<52.55]extra2 = [i for i in s if i>61 and i<68]extra3 = [i for i in s if i>73]

我想知道这些值的索引。有更好的方法吗?

谢谢!

python pandas
1个回答
0
投票
您可以使用loc访问一组行并返回另一个Series,然后可以使用它检查索引。

import pandas as pd test = pd.Series({ 1: 5.0, 2: 4.5, 3: 8.2, 4: 9.0, 5: 6.7 }) extra = test.loc[lambda x: x < 8] # or extra = test[test < 8] # more concise but potentially slower print(extra) # 1 5.0 # 2 4.5 # 5 6.7 # dtype: float64

有几种方法可以执行,并且性能可能会有所不同,如here所述。 
© www.soinside.com 2019 - 2024. All rights reserved.