简体   繁体   中英

How do you get a list of indexes with certain criteria in a dataframe?

For example, I have a Dataframe with non-negative values throughout, but the missing values have been encoded as negative. I want to get all those indexes.

Use boolean indexing with df.index :

df = pd.DataFrame({
         'B':[-4,5,4,5,5,-4],
         'C':[7,8,9,4,2,3],
         'D':[1,3,5,-7,1,0],
         'E':[5,-3,6,9,2,4],

})

print (df)
   B  C  D  E
0 -4  7  1  5
1  5  8  3 -3
2  4  9  5  6
3  5  4 -7  9
4  5  2  1  2
5 -4  3  0  4

idx = df.index[df.lt(0).any(axis=1)]
print (idx)
Int64Index([0, 1, 3, 5], dtype='int64')

Explanation :

First compare all values by DataFrame.lt ( < ):

(similar functions are gt , le , ge , ne , eq )

print (df.lt(0))
       B      C      D      E
0   True  False  False  False
1  False  False  False   True
2  False  False  False  False
3  False  False   True  False
4  False  False  False  False
5   True  False  False  False

And then check at least one True value per row by DataFrame.any :

print (df.lt(0).any(axis=1))
0     True
1     True
2    False
3     True
4    False
5     True
dtype: bool

You can calculate the row-wise minimum and check if it is less than 0:

res = df.index[df.min(1) < 0]

# data from @jezrael    
# Int64Index([0, 1, 3, 5], dtype='int64')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM