【发布时间】:2021-01-07 20:38:04
【问题描述】:
我有一个包含以下列的数据框:
Acct Num、通讯日期、打开日期
对于每个开立的账户,我被要求回顾其中发生的所有通信 该帐户的开放日期为 30 天,然后将以下积分分配给对应关系:
Forty-twenty-forty: Attribute 40% (0.4 points) of the attribution to the first touch,
40% to the last touch, and divide the remaining 20% between all touches in between
所以我知道 apply 和 group by 功能,但这超出了我的工资等级。 我必须按帐户分组,条件是基于 2 列相互比较, 我必须这样做才能获得对应的总数,我猜它们也必须进行排序,因为将点分配给对应的以下步骤取决于它们发生的顺序。
我想有效地做到这一点,因为我有很多行,我知道 apply() 可以快速运行,但是当我在行级操作时 应用我正在尝试做的事情甚至有点复杂。
感谢任何帮助,因为我不擅长 pandas。
编辑 根据要求
Acct, ContactDate, OpenDate, Points (what I need to calculate)
123, 1/1/2018, 1/1/2021, 0 (because correspondance not within 30 days of open)
123, 12/10/2020, 1/1/2021, 0.4 (first touch gets 0.4)
123, 12/11/2020, 1/1/2021, 0.2 (other 'touches' get 0.2/(num of touches-2) 'points')
123, 12/12/2020, 1/1/2021, 0.4 (last touch gets 0.4)
456, 1/1/2018, 1/1/2021, 0 (again, because correspondance not within 30 days of open)
456, 12/10/2020, 1/1/2021, 0.4 (first touch gets 0.4)
456, 12/11/2020, 1/1/2021, 0.1 (other 'touches' get 0.2/(num of touches-2) 'points')
456, 12/11/2020, 1/1/2021, 0.1 (other 'touches' get 0.2/(num of touches-2) 'points')
456, 12/12/2020, 1/1/2021, 0.4 (last touch gets 0.4)
【问题讨论】:
-
不清楚你所说的分配积分是什么意思。你能举例说明你希望输出的样子吗?
-
是的。我将发布编辑
-
如果第一次或最后一次触摸与其他触摸相同会发生什么?例如如果一个帐户有 3 个对应关系并且它们都发生在同一天怎么办?如果这不是问题,那么下面乔纳森的回答似乎就可以了!
标签: python pandas dataframe apply