geistmate
geistmate

Reputation: 555

pandas How to remove all rows that contain all false using iloc

I have a dataframe where I want to remove all rows with all zeros.

df = pd.DataFrame(data={
    'a': [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0],
    'b': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    'c': [0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0],
    'd': [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
    'e': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    'f': [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
})

Using .ix I would do df.ix[(df>0).any(1)]

    a   b   c   d   e   f
1   0   0   0   0   0   1
5   0   0   5   0   0   0
6   1   0   0   0   0   0
8   0   0   0   1   0   0
9   1   0   0   0   0   0

to get the correct output but when I try with iloc it gives me iLocation based boolean indexing on an integer type is not available. I am trying to use iloc because ix is now depreciated.

df.iloc[(df>0).any(1)]

Upvotes: 0

Views: 315

Answers (3)

Keyur Togadiya
Keyur Togadiya

Reputation: 1

There is no need to use .iloc here you can just use df[(df>0).any(1)], it will give you the desired output.

However, if you want to do it with .iloc here's what to do: What you are trying to do is pass a pandas Series object to .iloc, thus getting an error.

Convert the pandas series to list 1st then pass to .iloc i.e. this >>

df.iloc[list((df>0).any(1))]

Upvotes: 0

NikosVavlas
NikosVavlas

Reputation: 11

Alternatively you can iterate the df rows and check if all row values are equal to 0. If this is the case, you drop the corresponding row. For example:

for index, row in df.iterrows():
  if (all(values == 0 for values in row.values)):
    df=df.drop(index)

The above code produces your desired output.

Upvotes: 0

BENY
BENY

Reputation: 323376

Remove iloc

df[(df>0).any(1)] # or df.loc[(df>0).any(1)]
Out[185]: 
   a  b  c  d  e  f
1  0  0  0  0  0  1
5  0  0  5  0  0  0
6  1  0  0  0  0  0
8  0  0  0  1  0  0
9  1  0  0  0  0  0

Upvotes: 3

Related Questions