python - 如何获取过滤后的 Panda 系列的索引?

标签 python pandas

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

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

GEOM_190_190 0.00
GEOM_190_192 1.91
GEOM_190_194 2.54
GEOM_190_196 4.90
GEOM_190_198 6.03
GEOM_190_200 6.92
GEOM_190_202 8.60
GEOM_190_204 10.06
GEOM_190_206 11.34
GEOM_190_208 12.43
数据类型: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]

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

谢谢!

最佳答案

您可以使用loc访问一组行并返回另一个系列,然后您可以使用它来检查索引。

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

编辑:要在一个 loc 中执行多个条件,请尝试如下操作:

test.loc[(test > 5) & (test < 8)]
# 5    6.7
# dtype: float64 

关于python - 如何获取过滤后的 Panda 系列的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60395300/

相关文章:

javascript - 运行 Selenium Webdriver (Python) 测试时如何加载 Firefox 并禁用 javascript?

python - 如何通过 Python 抓取动态网页

python - 在 python 中分割文件路径字符串时出错

python - 使用 Django-socialregistration 的 Facebook Connect API 错误 191

python - 在数据帧上执行 for 循环的更快替代方案?

python - 在 AWS Transcribe 中实时获取 BadRequestException

Python Pandas 在 DataFrame 中聚合系列数据

python - Groupby 与 TimeGrouper 'backwards'

python - 让 x 轴网格显示在 matplotlib 中

python - Pandas 按组将列转换为多个