【发布时间】:2021-07-15 00:53:37
【问题描述】:
我想通过customer_id 计算num_opens_at_campaign_send。这取决于客户在发送每个活动之前打开的活动数量。
我无法找出在 pandas 中执行此操作的最佳方法,因此我们将不胜感激。我正在考虑使用 groupby customer_id 和 apply 函数将每个 campaign_sent 日期与该列中的所有其他日期进行比较,但我不确定获取行数以计算活动数量的精确方法每次发送广告活动时,客户已打开。
数据框如下:
| customer_id | campaign_id | campaign_sent | opened |
|---|---|---|---|
| a | 1234 | 2021-01-01 | True |
| b | 1234 | 2021-01-01 | True |
| c | 1234 | 2021-01-01 | False |
| a | 2222 | 2021-02-01 | True |
| b | 2222 | 2021-02-01 | False |
| c | 2222 | 2021-02-01 | True |
| a | 3333 | 2021-03-01 | True |
| b | 3333 | 2021-03-01 | False |
| c | 3333 | 2021-03-01 | True |
想要的输出是:
| customer_id | campaign_id | campaign_sent | num_opens_at_campaign_send |
|---|---|---|---|
| a | 1234 | 2021-01-01 | 0 |
| b | 1234 | 2021-01-01 | 0 |
| c | 1234 | 2021-01-01 | 0 |
| a | 2222 | 2021-02-01 | 1 |
| b | 2222 | 2021-02-01 | 1 |
| c | 2222 | 2021-02-01 | 0 |
| a | 3333 | 2021-03-01 | 2 |
| b | 3333 | 2021-03-01 | 1 |
| c | 3333 | 2021-03-01 | 1 |
所以对于第一个广告系列,num_opens_at_campaign_send 全部为 0,因为之前没有广告系列。
例如,customer_id 'b' 在发送 campaign_id 3333 时打开了 1 封邮件,因为他们打开了第一个活动 (1234) 但没有打开第二个活动 (2222) 电子邮件。
【问题讨论】: