Do not apply lambda function to missing values

Submitted 3 years, 7 months ago
Ticket #137
Views 259
Language/Framework Python
Priority Medium
Status Closed

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
Submitted on Sep 10, 20
add a comment

1 Answer

Verified

To fix it, you could simply wrap the isnull statement with np.all:

df[["initial_diagnosis"]].apply(lambda x: my_func("initial_diagnosis") if(np.all(pd.notnull(x[1]))) else x, axis = 1)

Now you'll see that np.all(pd.notnull(['foo', 'bar'])) is indeed True.

Submitted 3 years, 6 months ago


Latest Blogs