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.
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.
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]})
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With