I was wondering how I can remove all indexes that containing negative values inside their column. I am using Pandas DataFrames
.
Documentation Pandas DataFrame
Format:
Myid - valuecol1 - valuecol2 - valuecol3 -... valuecol30
So my DataFrame
is called data
I know how to do this for 1 column:
data2 = data.index[data['valuecol1'] > 0]
data3 = data.ix[data3]
So I only get the ids where valuecol1 > 0
, how can I do some kind of and
statement?
valuecol1 && valuecol2 && valuecol3 && ... && valuecol30 > 0
?
Using iloc – You can use negative index to select a row when using iloc as this method is used to select rows and columns using index number.
Select Pandas Rows With Column Values Greater Than or Smaller Than Specific Value. To select Pandas rows with column values greater than or smaller than specific value, we use operators like > , <= , >= while creating masks or queries.
You can use all
to check an entire row or column is True:
In [11]: df = pd.DataFrame(np.random.randn(10, 3))
In [12]: df
Out[12]:
0 1 2
0 -1.003735 0.792479 0.787538
1 -2.056750 -1.508980 0.676378
2 1.355528 0.307063 0.369505
3 1.201093 0.994041 -1.169323
4 -0.305359 0.044360 -0.085346
5 -0.684149 -0.482129 -0.598155
6 1.795011 1.231198 -0.465683
7 -0.632216 -0.075575 0.812735
8 -0.479523 -1.900072 -0.966430
9 -1.441645 -1.189408 1.338681
In [13]: (df > 0).all(1)
Out[13]:
0 False
1 False
2 True
3 False
4 False
5 False
6 False
7 False
8 False
9 False
dtype: bool
In [14]: df[(df > 0).all(1)]
Out[14]:
0 1 2
2 1.355528 0.307063 0.369505
If you only want to look at a subset of the columns, e.g.[0, 1]
:
In [15]: df[(df[[0, 1]] > 0).all(1)]
Out[15]:
0 1 2
2 1.355528 0.307063 0.369505
3 1.201093 0.994041 -1.169323
6 1.795011 1.231198 -0.465683
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