【问题标题】:Counting days on a non-unique index在非唯一索引上计算天数
【发布时间】:2022-08-22 23:53:07
【问题描述】:

我有一段代码可以很好地在 Plan_Start 变量之间按顺序排列天数。

设置代码:

tx1 = pd.DataFrame({\'Patient\':[123,456,789,789,101],
             \'Plan\':[\'Drug1\',\'Drug43\',\'Drug_abc\',\'Drug_xyz\',\'Drug_324\'],
             \'Plan_Start\':[\'4/21/2021\',\'6/11/2021\',\'7/7/2021\',\'7/12/2021\',\'9/20/2021\'],
             \'Plan_End\':[\'1/1/2030\',\'7/20/2021\',\'7/12/2022\',\'7/31/2021\',\'9/20/2022\']})
tx1[\'Plan_Start\'] = pd.to_datetime(tx1[\'Plan_Start\'])
tx1[\'Plan_End\'] = pd.to_datetime(tx1[\'Plan_End\'])

tx1

当您运行以下代码时:

tx1.set_index(\'Plan_Start\').groupby([\'Patient\']).resample(\'D\').ffill().reset_index(level=0, drop=True).reset_index()

准确地产生了这个:

但是,在 Plan_Start 变量可能具有相同日期的情况下(通常是这种情况,因为患者开始治疗并且必须在同一天结束治疗,否则无效),这将不起作用。

tx2 = pd.DataFrame({\'Patient\':[123,456,789,789,789,101],
             \'Plan\':[\'Drug1\',\'Drug43\',\'Drug_abc\',\'Drug_xyz\',\'Drug_123\',\'Drug_324\'],
             \'Plan_Start\':[\'4/21/2021\',\'6/11/2021\',\'7/7/2021\',\'7/7/2021\',\'7/17/2021\',\'9/20/2021\'],
             \'Plan_End\':[\'1/1/2030\',\'7/20/2021\',\'7/7/2022\',\'7/17/2021\',\'07/31/2021\',\'9/20/2022\']})
tx2

现在这段代码:

tx2.set_index(\'Plan_Start\').groupby([\'Patient\']).resample(\'D\').ffill().reset_index(level=0, drop=True).reset_index()

现在抛出这个错误:

ValueError: cannot reindex a non-unique index with a method or limit

如何包含重复的第 2 行(对于 2021 年 7 月 7 日开始和 2021 年 7 月 7 日结束),然后从 2021 年 7 月 7 日 Plan_Start 重新开始计数到 ​​2021 年 7 月 17 日的下一个 Plan_Start?

    标签: python pandas datetime


    【解决方案1】:

    代码

    mask = tx2.duplicated(['Patient', 'Plan_Start'], keep='last')
    
    tx2_resampled = (
        tx2[~mask]
        .set_index('Plan_Start')
        .groupby('Patient', group_keys=False)
        .resample('D').ffill().reset_index()
    )
    
    tx2_result = pd.concat([tx2[mask], tx2_resampled])\
                   .sort_values(['Patient', 'Plan_Start'], ignore_index=True)
    

    逻辑

    该解决方案背后的核心思想是首先将重复行通过PatientPlan_Start 分开,然后groupbyresample 不重复行,最后concat 重复行回到重新采样的数据帧

    结果

        Patient      Plan Plan_Start   Plan_End
    0       101  Drug_324 2021-09-20 2022-09-20
    1       123     Drug1 2021-04-21 2030-01-01
    2       456    Drug43 2021-06-11 2021-07-20
    3       789  Drug_abc 2021-07-07 2022-07-07
    4       789  Drug_xyz 2021-07-07 2021-07-17
    5       789  Drug_xyz 2021-07-08 2021-07-17
    6       789  Drug_xyz 2021-07-09 2021-07-17
    7       789  Drug_xyz 2021-07-10 2021-07-17
    8       789  Drug_xyz 2021-07-11 2021-07-17
    9       789  Drug_xyz 2021-07-12 2021-07-17
    10      789  Drug_xyz 2021-07-13 2021-07-17
    11      789  Drug_xyz 2021-07-14 2021-07-17
    12      789  Drug_xyz 2021-07-15 2021-07-17
    13      789  Drug_xyz 2021-07-16 2021-07-17
    14      789  Drug_123 2021-07-17 2021-07-31
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 2019-02-23
      • 1970-01-01
      相关资源
      最近更新 更多