Reputation: 11
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.
Upvotes: 0
Views: 437
Reputation: 164653
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')
Upvotes: 1
Reputation: 862601
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
Upvotes: 1