我需要创建一个新列来建议客户是新顾客还是老顾客。
为此,我想针对“电话”中的每个唯一值,检查“日期”列中是否存在一个或多个日期。
Phone Date
0 a 1
1 a 1
2 a 2
3 b 2
4 b 2
5 c 3
6 c 2
7 c 1
[新用户是指只有一个唯一的(电话,日期)夫妇与同一电话配对的用户。我想要的结果看起来像:
Phone Date User_type
0 a 1 recurrent
1 a 1 recurrent
2 a 2 recurrent
3 b 2 new
4 b 2 new
5 c 3 recurrent
6 c 2 recurrent
7 c 1 recurrent
我设法用python用几行代码来做到这一点,但我的老板想坚持要我在Tableau中做到这一点。
我知道我需要使用计算所得的字段,仅此而已。
如果有帮助,这是我的python代码,可以执行相同的操作:
import numpy as np
import pandas as pd
for item in set(data.Phone):
if len(set(data[data.Phone == item]['Date'])) == 1:
data.loc[data.Phone == item, 'type_user'] = 'new'
elif len(set(data[tata.Phone == item]['Date'])) > 1:
data.loc[data.Phone == item, 'type_user'] = 'recurrent'
else:
data.loc[data.Phone == item, 'type_user'] = np.nan