0

I want to filter though my dataset using conditions. I tried using .isin() function but i got an empty dataframe when filtering an np.array float list. Here is my code

f1 = []
f2 = []
f3 = []
f4 = []
for c in range(100):
    x = (2 * c) + 28
    y = ((9 * c)/5) + 32
    z = abs(x-y)
    f1.append(x)
    f2.append(y)
    f3.append(c)
    f4.append(z)
columns=['Temp in Celsius', 'Norm Temp in F', 'Make shift Temp in F', 'Absolute Diff btw formulas']
df = pd.DataFrame([f3,f2,f1,f4]).transpose()
df.columns=columns
print(df[df['Absolute Diff btw formulas'].isin(np.arange(0.1, 0.9, 0.1))])

The values i am searching for in the column 'Absolute Diff btw formulas' is actually present because when i run df[15:25] i get this

print(df[15:25])
    Temp in Celsius  Norm Temp in F  Make shift Temp in F  Absolute Diff btw formulas
15             15.0            59.0                  58.0                         1.0
16             16.0            60.8                  60.0                         0.8
17             17.0            62.6                  62.0                         0.6
18             18.0            64.4                  64.0                         0.4
19             19.0            66.2                  66.0                         0.2
20             20.0            68.0                  68.0                         0.0
21             21.0            69.8                  70.0                         0.2
22             22.0            71.6                  72.0                         0.4
23             23.0            73.4                  74.0                         0.6
24             24.0            75.2                  76.0                         0.8

So, how can i go about it using the .isin() function or any conditional statement. The goal is to printout the dataframe with df['Absolute Diff btw formulas'] values of range 0.1 to 0.9 .Thanks

Achebe Peter
  • 131
  • 8

2 Answers2

1

use -

print(df[(df['Absolute Diff btw formulas']>=0.1) & (df['Absolute Diff btw formulas']<=0.9)])

  • Thank you Rohit, I actually tried this but without the parenthesis. Can this be done using the isin() function ? – Achebe Peter Jun 10 '20 at 11:22
  • 1
    The alternative to isin() is between(), for continuous values. In this case, you can use - df[df['Absolute Diff btw formulas'].between(0.1,0.9)] – Rohit upadhyay Jun 10 '20 at 12:09
1

You can use pandas.query:

print( df.query('0.1 <= `Absolute Diff btw formulas` <= 0.9') )

Prints:

    Temp in Celsius  Norm Temp in F  Make shift Temp in F  Absolute Diff btw formulas
16             16.0            60.8                  60.0                         0.8
17             17.0            62.6                  62.0                         0.6
18             18.0            64.4                  64.0                         0.4
19             19.0            66.2                  66.0                         0.2
21             21.0            69.8                  70.0                         0.2
22             22.0            71.6                  72.0                         0.4
23             23.0            73.4                  74.0                         0.6
24             24.0            75.2                  76.0                         0.8
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91