【问题标题】:check for dates in couple of columns and create a new column in python检查几列中的日期并在python中创建一个新列
【发布时间】:2019-05-09 07:01:03
【问题描述】:

我是 Python 新手。在数据框中,有两列,DOB(出生日期)和 DOD(死亡日期)。我想在数据框中创建一个名为“IS_ALIVE”的新分类列。当 DOB 被填充且 DOD 为空时,此新字段的条件 IS_ALIVE 应为“1”;当 DOB 和 DOD 都不为空时,IS_ALIVE 应该为“0”,这意味着,某个日期填充了 DOD。

我搜索并尝试了多种方法,但没有运气。请帮帮我。

【问题讨论】:

    标签: python python-3.x pandas data-science feature-engineering


    【解决方案1】:

    您可以使用&(按位与)链接的isnanotna 创建布尔掩码,并将True/False 的整数转换为1/0

    df['IS_ALIVE'] = (df.DOB.notna() & df.DOD.isna()).astype(int)
    #for old versions of pandas
    #df['IS_ALIVE'] = (df.DOB.notnull() & df.DOD.isnull()).astype(int)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 2022-08-12
      • 1970-01-01
      相关资源
      最近更新 更多