英文:
How does one create a pandas column (binary) based on whether or not another column contains dates
问题
我正在分析客户流失情况。我的数据集包含多年来留下或离开的客户。我需要根据['CHURN_DATE']列中是否有日期来创建一个['CHURN_FLAG']列。如果['CHURN_DATE']列中有日期,那么客户就流失了。
当前数据框:
CHURNdate |
---|
2023-1-1 |
NaT |
期望的结果:
CHURNdate | CHURNflag |
---|---|
2023-1-1 | 1 |
NaT | 0 |
我创建了一个['TODAY_DATE']列,并尝试通过评估['CHURNdate']<['TODAY_DATE']是否成立来解决这个问题,如果成立,则填充二进制1,否则填充0。以下是代码:
df2['CHURNflag'] = np.where(df2['CHURNdate']<df2['TODAY_DATE'], 0, 1)
自然而然,它没有起作用。:( 数据类型是datetime64。
英文:
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
答案1
得分: 0
churn['CHURNflag'] = np.where(churn['CHURNdate'].isna(), 0, 1)
Out[24]:
CHURNdate CHURNflag
0 2023-01-01 1
1 NaN 0
英文:
churn['CHURNflag'] = np.where(churn['CHURNdate'].isna(), 0, 1)
Out[24]:
CHURNdate CHURNflag
0 2023-01-01 1
1 NaN 0
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论