创建一个 pandas 列(二进制),根据另一列是否包含日期。

huangapple go评论64阅读模式
英文:

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[&#39;CHURNflag&#39;] = np.where(df2[&#39;CHURNdate&#39;]&lt;df2[&#39;TODAY_DATE&#39;], 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[&#39;CHURNflag&#39;] = np.where(df2[&#39;CHURNdate&#39;]&lt;df2[&#39;TODAY_DATE&#39;], 0, 1)

Naturally, it didn't work. 创建一个 pandas 列(二进制),根据另一列是否包含日期。 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[&#39;CHURNflag&#39;] = np.where(churn[&#39;CHURNdate&#39;].isna(), 0, 1)

Out[24]: 
    CHURNdate  CHURNflag
0  2023-01-01          1
1         NaN          0

huangapple
  • 本文由 发表于 2023年6月16日 05:14:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485562.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定