0

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?

2
  • @Bharathshetty it returns me None with inplace=True Commented Oct 6, 2017 at 14:18
  • I thought its dropna sorry. You can do that without using drop. Check if my answer helps Commented Oct 6, 2017 at 14:20

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

4
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

4 Comments

whats ^m I'm new to that pattern?
Very smart, took me a sec to figure out astyp(bool) here :)
@Bharathshetty, it's a RegEx - meaning string that starts with "m"
Oh shit I imagined an escape character behind m I thought its like ^\m. My bad I feel stupid now haha
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.