I have a dataframe with the patient's diagnosis in a column and using pandas I want to dichotomize the diagnosis ==> ISM, non ISM. I tried this
df["initial_diagnosis"] = df["initial_diagnosis"].apply(lambda x: x if x=="ISM" else "non ISM")
But it is assigning "non ISM" also to missing values. Is there a way to do the same and keep the missing values as they are?
The column that I'm trying to code looks like this:
initial_diagnosis
ISM
ISM
WDSM
NaN
ISM
SSM
CM
ASM
ISM
To fix it, you could simply wrap the isnull statement with
np.all
:Now you'll see that
np.all(pd.notnull(['foo', 'bar']))
is indeedTrue
.