Picking up column values based on two conditions

Viewed 2953

I have the following text output, my goal is to only select values of column 2 when the values in column 1 are greater than 1 but less than or equal to 4. So I am looking for Python to print out Column 2 values as [-6,0,-4] because only these values meet the criteria of column 1.

  1. 1 2
  2. 2 -6
  3. 3 0
  4. 4 -4
  5. 5 100

I tried the following approach.

import pandas as pd
import numpy as np
data= pd.read_table('/Users/Hrihaan/Desktop/A.txt', dtype=float, header=None, sep='\s+').values
x=data[:,0]
y=np.where(1< x<= 4, data[:, 1], np.nan)
print(y)

I received the following error: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Any suggestion would be really helpful.

2 Answers
Related