简体   繁体   中英

How to drop string NULL values in pandas dataframe?

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.

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

You can use this:

df = df[(df.astype(str).applymap(lambda x: x.lower()) != 'null').all(axis=1)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM