vamsi krishna
vamsi krishna

Reputation: 21

how to find out rows that have one or more values as zero in any column of data set in python

Suppose say I have a dataframe with 20 rows(observations) and 4 columns.

Out of 20 rows, 5 rows have zeroes in 2 columns. How to find out which rows have zeroes as values in any column of dataframe in python?

Upvotes: 2

Views: 4816

Answers (2)

ZealousWeb
ZealousWeb

Reputation: 1751

You can try something like this to get the list of indices of rows which have values 0:

df[(df.values == 0).any(axis=1)].index.values.tolist()

Upvotes: 2

mandulaj
mandulaj

Reputation: 763

You can check which elements are zeros with df == 0. Then you can apply the .any across axis 1 in order to see which rows have any column equal to 0:

(df == 0).any(axis=1)

If you want to get the row indexes for all elements where that is true, you can do something like this:

df[(df == 0).any(axis=1)].index

Upvotes: 2

Related Questions