Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How does one create a pandas column (binary) based on whether or not another column contains dates

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading