0

I have string 'NULL' values to drop. But I could not do it. Below is my data set and I need to drop row 1 and 4. How can I do that? Thank you.

df=pd.DataFrame({'A':['NULL','x2','x3','x4],'B':['w1','w2','w3','NULL']})


     A     B
    NULL   w1
    x2     w2
    x3     w3
    x4     NULL

Thank you.

1
  • The rows are not null, they are strings. One is the string 'null' and other one is 'NULL'. I belive the dataframe is this: df=pd.DataFrame({'A':[None,'x2','x3','x4'],'B':['w1','w2','w3',None]}) Commented Sep 24, 2019 at 9:59

2 Answers 2

1

You can use this:

df = df[(df.astype(str).applymap(lambda x: x.lower()) != 'null').all(axis=1)]
Sign up to request clarification or add additional context in comments.

1 Comment

@melik With the code you posted in the question (with 'NULL' / 'Null' / 'null' strings) it works fine. If your data has np.NaN values, it doesn't work, but then you should ask a different question.
1

Using .str.contains - to Test if pattern or regex is contained within a string of a Series with | for OR bitwise.

df=pd.DataFrame({'A':['NULL','x2','x3','x4'],'B':['w1','w2','w3','NULL']})
m = df['A'].str.contains("NULL") | df['B'].str.contains("NULL")
df = df[~m]
print(df)

    A   B
1  x2  w2
2  x3  w3

OR

m = (df == 'NULL').any(1)
df = df[~m]
print(df)

    A   B
1  x2  w2
2  x3  w3

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.