Confucion
Confucion

Reputation: 101

Selecting Pandas DF between two values

I'm trying to subset a column of values that were extracted from a correlation matrix. I want to get values greater than 0.75 and less than -0.75. I tried the first line of code and it only gave me positive values greater than 0.75. The second line of code error'd out without a result.

Corr_matrix1 = Corr_matrix1[(Corr_matrix1['Coefficient'] >= abs(0.75))]

Corr_matrix1 = Corr_matrix1 [(Corr_matrix1 ['Coefficient'] >= 0.75) & (Corr_matrix1 ['Coefficient'] <= -0.75)]

Any help would be appreciated.

Upvotes: 0

Views: 127

Answers (2)

unltd_J
unltd_J

Reputation: 494

You can do this with the DataFrame.query method, one of my favorite features of pandas and it's pretty slept on. Here's an example;

df.corr().query(
'Coefficient <= -0.75'
'or Coefficient >= 0.75'
)

It's kind of odd, you pass the arguments as strings without commas in between multiple arguments. If you use a variable, you can use an f string.

Upvotes: 1

Related Questions