1

I have an issue where I have a dataframe data with multiple columns and I want to create a variable filter in the dataframe and assign the value 1 if activation_date is null else 0.

I have written this code but this is failing to get the results, everything is getting 0 irrespective if the dates are still present.

data['filter'] = [0 if x is not None else 1 for x in data['activation_dt']]

1 Answer 1

1

I think you need isnull for check None or NaNs and then convert True to 1 and False to 0 by astype(int):

data = pd.DataFrame({'activation_dt':[None, np.nan, 1]})
print (data)
   activation_dt
0            NaN
1            NaN
2            1.0

data['filter'] = data['activation_dt'].isnull().astype(int)
print (data)
   activation_dt  filter
0            NaN       1
1            NaN       1
2            1.0       0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.