user5779223 user5779223 - 5 months ago 14
Python Question

Get the number index through boolean opearator in python-pandas

Now I can get the expected value with boolean operator:

mask1 = df.val > 10
mask2 = df.val < 5
c1 = df[mask1]
c2 = df[mask2]


And I wish to get the slice between each point in
c1
and
c2
, that is, given a data frame
df
as below:

val
0 9
1 12
2 5
3 2
4 11
5 9
6 9
7 3


The slices of
df[1:3]
and
df[4:7]
are what I want. First of all, I need to get the index
1
,
3
,
4
,
7
through
mask1
and
mask2
. Do you know how to do it?

Answer

I think you can use:

c1 = df[mask1].index
c2 = df[mask2].index

print (c1)
Int64Index([1, 4], dtype='int64')
print (c2)
Int64Index([3, 7], dtype='int64')

print (df[c1[0]:c2[0]])
   val
1   12
2    5

print (df[c1[1]:c2[1]])
   val
4   11
5    9
6    9

It is same as:

print (df[1:3])
   val
1   12
2    5

print (df[4:7])
   val
4   11
5    9
6    9

If you need slice by c1 or c2 use ix:

print (df.ix[c1])
   val
1   12
4   11

print (df.ix[c2])
   val
3    2
7    3
Comments