【发布时间】:2021-08-04 06:48:47
【问题描述】:
我有一个庞大的客户数据集,其中包含客户 ID、服务 ID、产品等内容。因此,我们衡量流失率的两种方法是在客户 ID 级别(如果整个客户离开)和在服务- ID 级别,他们可能会取消 5 项服务中的 2 项。
数据看起来像这样,我们可以看到
- Alligators 在 1 月底不再是客户,因为它们在 2 月没有任何行 (CustomerChurn)
- Aunties 在 1 月底不再是客户,因为他们在 2 月没有任何行 (CustomerChurn)
- 1 月和 2 月继续卖苹果和橙子 (ServiceContinue)
- Bricks 仍然是客户,但在 1 月底取消了两项服务 (ServiceChurn)
我正在尝试编写一些创建“流失”列的代码。我已经尝试过
- 从 2019 年 10 月开始使用 Set 手动获取 CustomerID 和 ServiceID 列表,然后将其与 2019 年 11 月进行比较,以找到流失的列表。这不是太慢,但似乎不是很 Pythonic。
谢谢!
data = {'CustomerName': ['Alligators','Aunties', 'Bricks', 'Bricks','Bricks', 'Bricks', 'Bricks', 'Bricks', 'Bricks', 'Bricks'],
'ServiceID': [1009, 1008, 1001, 1002, 1003, 1004, 1001, 1002, 1001, 1002],
'Product': ['Apples', 'Apples', 'Apples', 'Bananas', 'Oranges', 'Watermelon', 'Apples', 'Bananas', 'Apples', 'Bananas'],
'Month': ['Jan', 'Jan', 'Jan', 'Jan', 'Jan', 'Jan', 'Feb', 'Feb', 'Mar', 'Mar'],
'Year': [2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021],
'Churn': ['CustomerChurn', 'CustomerChurn', 'ServiceContinue', 'ServiceContinue', 'ServiceChurn', 'ServiceChurn','ServiceContinue', 'ServiceContinue', 'NA', 'NA']}
df = pd.DataFrame(data)
df
【问题讨论】: