Petr Petrov
Petr Petrov

Reputation: 4442

Pandas: remove empty strings from dataframe

I have dataframe

id    m1    m2    m3
111   20    0     12
222   0     0     0
333   3     1     18

I need to get only

id    m1    m2    m3
111   20    0     12
333   3     1     18

I use

df.drop(axis=0, how='all')

But it returns me full dataframe. How can I fix that?

Upvotes: 0

Views: 2242

Answers (3)

Priya G
Priya G

Reputation: 46

here,it can be done in simple way as follows.

df=df[df['id']!=222]

print(df)

id m1 m2 m3 0 111 20 0 12 2 333 3 1 18

Upvotes: 0

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210842

In [91]: ndf = df[df.filter(regex='^m').astype(bool).any(1)]

In [92]: ndf
Out[92]:
    id  m1  m2  m3
0  111  20   0  12
2  333   3   1  18

Upvotes: 4

Bharath M Shetty
Bharath M Shetty

Reputation: 30605

You can use boolean indexing i.e

ndf = df[~(df.set_index('id')==0).all(1).values]

Output:

   id  m1  m2  m3
0  111  20   0  12
2  333   3   1  18

Upvotes: 4

Related Questions