I have a column with dates called "Click Date". Basically, I'm trying to write an IF statement to return IF there is a date or not in the column. IF click date contains any information, then "1" else "0".
Asked
Active
Viewed 41 times
-1
-
1Somehow I understood your question as "Is there any date in the whole column?". If the question is indeed that, then: `(~df["click_dt"].isna()).sum() > 0` – Maria K Jul 18 '23 at 14:08
-
Welcome to Stack Overflow! Check out [ask] for tips like how to write a good title. – wjandrea Jul 20 '23 at 15:56
-
@Maria I'd do `df["click_dt"].notna().any()` – wjandrea Jul 20 '23 at 16:22
2 Answers
0
If you are using numpy, you can return 0 or 1 depending if there's a date in a specific column with the following code:
import numpy as np
df["new_col"]=np.where(df["click_date"].isnull(), 0, 1)

Broken_Window
- 2,037
- 3
- 21
- 47

TaxpayersMoney
- 669
- 1
- 8
- 26
-
I think that's equivalent to `df["click_date"].isnull().astype('int')` – wjandrea Jul 20 '23 at 16:23