Lets say I have the following dataframe:
ID | has_id_dummy
-----------------------
340 NaN
NaN NaN
NaN NaN
200 NaN
And I want to turn it into this DataFrame:
ID | has_id_dummy
-----------------------
340 1
NaN 0
NaN 0
200 1
To do this I came up with the following function:
def dummypopulator(x):
if x != np.nan:
return 1
return 0
which I call with the following line
df['has_id_dummy'] = df['ID'].apply(dummypopulator)
But the value is then set to 1 for all rows, even the rows that dont have an ID and should be 0.
ID | has_id_dummy
-----------------------
340 1
NaN 1
NaN 1
200 1
I tried calling the function with a seperate lambda as I saw in an example:
df['has_id_dummy'] = df['ID'].apply(lambda x: dummypopulator(x))
Yet the result is the same.
I feel like I am missing a very obvious error, but for the life of me cant figure out why it wont work. Does anyone know what I am doing wrong?