Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picking up column values based on two conditions

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.

like image 444
Hrihaan Avatar asked Dec 12 '25 07:12

Hrihaan


2 Answers

1st using .loc

df.loc[(df.a>1)&(df.a<=4), 'b']
Out[316]: 
1   -6
2    0
3   -4

2nd base on you own approach

np.where((df.a<= 4)&(df.a>1), df.b,np.nan)

Out[322]: array([ nan,  -6.,   0.,  -4.,  nan])

data input :

df = pd.DataFrame({"a":np.arange(1,6), "b":[2,-6,0,-4,100]})
like image 117
BENY Avatar answered Dec 13 '25 22:12

BENY


There are a few ways to accomplish this with Pandas. One simple solution is to use query():

import pandas as pd
import numpy as np

data = {"a":np.arange(1,6), "b":[2,-6,0,-4,100]}
df = pd.DataFrame(data)

print(df)
   a    b
0  1    2
1  2   -6
2  3    0
3  4   -4
4  5  100

Now filter using query, and select column b:

df.query('1 < a <= 4').b

1   -6
2    0
3   -4
Name: b, dtype: int64
like image 45
andrew_reece Avatar answered Dec 13 '25 21:12

andrew_reece