0

当一个客户有多个订阅时,它就会被复制。我想为整个客户状态生成一个 new_status,而不是为每个订阅生成一个新状态:一个重新激活订阅的客户和一个取消一个订阅但仍然有另一个活动订阅的客户。

东风:

Customer | Status  | Canceled_at | Created  | New_status
 X       | Active  |             |8/9/2017  |
 X       |Canceled |  8/3/2017   |6/19/2017 |             
 Y       | Active  |             |2/13/2019 |
 Y       |Canceled | 11/28/2018  |10/14/2018|
 Z       | Active  |             |3/29/2018 |
 Z       |Canceled | 8/8/2018    |7/10/2018 |
 A       |Canceled | 9/2/2018    |7/10/2018 |          
 A       |Canceled | 9/29/2018   |7/12/2018 |
 A       |Active   |             |5/31/2018 |

这些情况的条件是:如果取消重复的“canceled_at”日期>活动的“created”日期:如果取消重复的“canceled_at”日期<活动:new_status 将是“重新激活”

期望的输出:

Customer | Status  | Canceled_at | Created  | New_status
 X       | Active  |             |8/9/2017  |Reactivate
 X       |Canceled |  8/3/2017   |6/19/2017 |Reactivate              
 Y       | Active  |             |2/13/2019 |Reactivate
 Y       |Canceled | 11/28/2018  |10/14/2018|Reactivate
 Z       | Active  |             |3/29/2018 |Downgrade
 Z       |Canceled | 8/8/2018    |7/10/2018 |Downgrade
 A       |Canceled | 9/2/2018    |7/10/2018 |Downgrade           
 A       |Canceled | 9/29/2018   |7/12/2018 |Downgrade
 A       |Active   |             |5/31/2018 |Downgrade
4

1 回答 1

1

我太新了,无法发表评论,但我需要更多信息,为什么“Y”客户重新激活?也许我不明白您的解释,因为客户“A”处于类似情况,而您给了它“降级”。也许只是重新输入您的问题,但假装它是一个 8 岁的孩子阅读(我)。

这是你想要的代码,它可以工作:

#convert columns to dates
df['Canceled_at'] = pd.to_datetime(df['Canceled_at'])
df['Created'] = pd.to_datetime(df['Created'])

#make customer a list so we can loop through it
customer = list(df['Customer'].drop_duplicates())

#super awesome for loop that give us the largest date (this is the part where maybe your logic is different than what I read it as)
for c in customer:
    df.loc[(df['Customer'] == c), 'Most Recent Cancel'] = df.loc[(df['Customer'] == c)]['Canceled_at'].max()
    df.loc[(df['Customer'] == c), 'Most Recent Created'] = df.loc[(df['Customer'] == c)]['Created'].max()

#Make 'New_status' column
df.loc[(df['Most Recent Created'] > df['Most Recent Cancel']), 'New_status'] = 'Reactivate'
df.loc[(df['New_status'] != 'Reactivate'), 'New_status'] = 'Downgrade'
于 2019-03-19T19:53:09.623 回答