1

I am working with 13548 continuous values, my data is constructed on the following way:

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
dtype: float64

I apply the following filters:
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]

I would like to know the indexes of those values. Is there a better way to do it?

Thanks!

TYSH
  • 53
  • 7

1 Answers1

0

You can use loc to access a group of rows and return another Series, which you can then use to inspect the index.

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

There are a few ways to do this and the performance can differ as described here.

EDIT: to do multiple conditions in one loc try something like the following:

test.loc[(test > 5) & (test < 8)]
# 5    6.7
# dtype: float64 
totalhack
  • 2,298
  • 17
  • 23