【问题标题】:How can I get the indexes of a filtered Panda series?如何获取过滤后的 Panda 系列的索引?
【发布时间】:2020-02-25 23:43:13
【问题描述】:

我正在使用 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]

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

谢谢!

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您可以使用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 
    

    【讨论】:

    • 非常感谢,但是对于 x 介于 61 和 68 之间的情况,我仍然无法做到。
    猜你喜欢
    • 1970-01-01
    • 2018-06-03
    • 2016-06-11
    • 2013-10-03
    • 2019-10-31
    • 1970-01-01
    • 2015-09-07
    • 2014-10-03
    • 2018-01-19
    相关资源
    最近更新 更多