SteveS
SteveS

Reputation: 4040

How to remove missing values from a dataframe when it's defined in various ways ('NONE', None, 'EMPTY')?

I have the following dataframe:

pd.DataFrame({"name": ['Alfred', 'NONE', 'Catwoman'],
              "toy": [np.nan, 'Batmobile', 'EMPTY'],
              "kuku": [None, 8, 0]})

Now I want to remove all missing values - None or 'NONE' or 'EMPTY. How can I instruct pandas to remove variants of missing data: None, NONE and 'EMPTY' and any other variant (which is predefined as such)?

Upvotes: 0

Views: 512

Answers (1)

Onyambu
Onyambu

Reputation: 79208

df.replace('NONE', np.nan).dropna()

       name    toy  kuku
2  Catwoman  EMPTY   0.0

if you just want to replace all those:

df.replace({'NONE':np.nan, 'EMPTY':np.nan, 'None':np.nan})

       name        toy  kuku
0    Alfred        NaN   NaN
1       NaN  Batmobile   8.0
2  Catwoman        NaN   0.0

then you can use .dropna(axis = ) to drop the na values

Upvotes: 1

Related Questions