I am new to python and pandas
On the below data frame ,I need to the drop the columns which are totally "None" , with "blanks and None", but not the columns with values and None
On the above table, I want Column A and C to be dropped because they are totally "None" or "blank and None", but Column B has some valid data at least in 3 cells, it should not be disturbed
how to give this condition in df.drop (pandas)
CodePudding user response:
You can test missing values NaN
and None
like Nonetype
by DataFrame.isna
, then possible strings by DataFrame.isin
, chain by |
for bitwise OR
and pass to DataFrame.loc
with invert mask for test if all values are Trues per columns (default axis=0
) by DataFrame.all
:
m = df.isna() | df.isin(['', 'None', 'none'])
df = df.loc[:, ~m.all()]
Or like comment, only in output are replaced values:
df = df.replace(['', 'None', 'none'],np.nan).dropna(axis=1, how='all')