Dropping rows from pandas dataframe based on floatpoint range

Viewed 24

Goal: remove all rows from series where value is within range -0.2 to 0.2. Datatype for values is float. Is there any way to do this that doesn't require converting the type?

Sample data:

index
2020-12-31 00:44:09 0.1515
2020-12-31 00:40:57 0.4404
2020-12-31 00:40:19 0.0000
2020-12-31 00:37:54 -0.2263
2020-12-31 00:23:38 -0.1234
1 Answers

Use Series.between with ~ for invert mask in boolean indexing if s is Series:

s = s[~s.between(-0.2, 0.2)]

If working with column col use:

df = df[~df['col'].between(-0.2, 0.2)]
Related