I am analyzing customer churn. My dataset contains years of customers that have stayed or left. I need to create a [‘CHURN_FLAG’] column based on whether or not there is a date in the [‘CHURN_DATE’] column. If there is a date in the [‘CHURN_DATE’] column then the customer churned.
Current data frame:
| CHURNdate |
|---|
| 2023-1-1 |
| NaT |
Desired:
| CHURNdate | CHURNflag |
|---|---|
| 2023-1-1 | 1 |
| NaT | 0 |
I created a column [‘TODAY_DATE’] and have attempted to solve by assessing if the [‘CHURNdate’] < [‘TODAY_DATE’] then the binary 1 would populate, else 0. Here is the code:
df2['CHURNflag'] = np.where(df2['CHURNdate']<df2['TODAY_DATE'], 0, 1)
Naturally, it didn’t work. 🙁 The datatypes are datetime64
>Solution :
churn['CHURNflag'] = np.where(churn['CHURNdate'].isna(), 0, 1)
Out[24]:
CHURNdate CHURNflag
0 2023-01-01 1
1 NaN 0