0

enter image description here

How do I drop all the rows that have a RET with absolute value greater than 0.10?

Tried using this, not sure what to do

df_drop = [data[data[abs(float('RET')) < 0.10]  
mosc9575
  • 5,618
  • 2
  • 9
  • 32
  • Please don't post data or code as an image. This is hard to copy'n'past and makes it more difficult to reproduce. – mosc9575 Feb 07 '23 at 21:15
  • Does this answer your question? [How to select rows in a DataFrame between two values, in Python Pandas?](https://stackoverflow.com/questions/31617845/how-to-select-rows-in-a-dataframe-between-two-values-in-python-pandas) – mosc9575 Feb 07 '23 at 21:24

3 Answers3

1

You can keep RET with absolute value greater than 0.10 by

data = data[abs(data['RET'].astype('float')) >= 0.10]
Ricardo
  • 691
  • 3
  • 11
0

You can use abs() and le() or lt() to filter for the wanted values.

df_drop = data[data['RET'].abs().lt(0.10)]

Also consider between(). Select a appropriate policy for inclusive. It can be "neither", "both", "left" or "right".

df_drop = data[data['RET'].between(-0.10, 0.10, inclusive="neither")]

Example

data = pd.DataFrame({
    'RET':[0.011806, -0.122290, 0.274011, 0.039013, -0.05044],
    'other': [1,2,3,4,5]
})

        RET  other
0  0.011806      1
1 -0.122290      2
2  0.274011      3
3  0.039013      4
4 -0.050440      5

Both ways aboth will lead to

        RET  other
0  0.011806      1
3  0.039013      4
4 -0.050440      5

All rows with an absolute value greater than 0.10 in RET are excluded.

mosc9575
  • 5,618
  • 2
  • 9
  • 32
0

I advice you to code it in two steps to figure it out better.

import pandas as pd

df = pd.DataFrame({'val':[5, 11, 89, 63],
                   'RET':[-0.1, 0.5, -0.04, 0.09],
                   })

# Step 1 : define the mask
m = abs(df['RET']) > 0.1
# Step 2 : apply the mask
df[m]

print(df)

Result

df[m]
   val  RET
1   11  0.5
Laurent B.
  • 1,653
  • 1
  • 7
  • 16