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]
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]
You can keep RET with absolute value greater than 0.10 by
data = data[abs(data['RET'].astype('float')) >= 0.10]
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")]
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.
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