Alex Rivera | Logout

Get indices that satisfy some criteria

Asked 2013-06-20T14:02:02.137
18

In Pandas, how can I get a list of indices of a series/dataframe for the entries that satisfy some property?

The following returns a Series object

my_dataframe.loc[:,'some_column'] == 'some_value'

that looks like this:

519     True
509    False
826    False
503    False
511    False
512    False
500    False
507    False
516     True
504    False
521    False
510    False
351    False
522    False
526    False
517    False
501    False

but what I want is two lists, one with

[519, 516]

and one with the rest of indices. How can I do this in Pandas?

Edit
Report

1 Answer

5

A slight variant on @Jeff's example:

In [18]: df
Out[18]: 
          A         B
0  0.319489  1.012319
1  0.494205 -0.918240
2  1.501922 -0.409661
3 -1.593702  0.705407
4 -0.735312  1.037567
5 -0.201132 -0.673124
6  1.237310 -0.877043
7 -0.946714  0.984164
8 -0.923548  0.415094
9  0.135281 -0.199951

In [14]  list1 =  df.index[df.loc[:, 'B'] > 0]

In [15]: list1
Out[15]: Int64Index([0, 3, 4, 7, 8], dtype=int64)

In [16]: list2 = df.index - list1

In [17]: list2
Out[17]: Int64Index([1, 2, 5, 6, 9], dtype=int64)
answered 2013-06-20T14:19:04.097

Your Answer