def active_status(data):
status = []
for i in range(18):
#若本月没有消费
if data[i] == 0:
if len(status) > 0:
if status[i-1] == 'unreg':
status.append('unreg')
else:
status.append('unactive')
else:
status.append('unreg')
#若本月消费
else:
if len(status) == 0:
status.append('new')
else:
if status[i-1] == 'unactive':
status.append('return')
elif status[i-1] == 'unreg':
status.append('new')
else:
status.append('active')
return status
purchase_stats = df_purchase.apply(active_status,axis=1)
purchase_stats
#返回结果
user_id
1 [new, unactive, unactive, unactive, unactive, ...
2 [new, unactive, unactive, unactive, unactive, ...
3 [new, unactive, return, active, unactive, unac...
4 [new, unactive, unactive, unactive, unactive, ...
5 [new, active, unactive, return, active, active...
...
23566 [unreg, unreg, new, unactive, unactive, unacti...
23567 [unreg, unreg, new, unactive, unactive, unacti...
23568 [unreg, unreg, new, active, unactive, unactive...
23569 [unreg, unreg, new, unactive, unactive, unacti...
23570 [unreg, unreg, new, unactive, unactive, unacti...
Length: 23570, dtype: object
df_purchase还是一个dataframe表格如下图(我的目的就是把下图的数字替换成一个个如"new"\"unreg"等的英文):