【发布时间】:2022-01-08 02:05:51
【问题描述】:
考虑如下数据框:
| customerid | segment |
|---|---|
| 1 | active |
| 1 | viewed |
| 1 | addtocart |
| 1 | transacted |
| 2 | active |
| 2 | viewed |
| 2 | addtocart |
| 3 | active |
| 3 | viewed |
| 4 | active |
| 5 | active |
如果我们看到:
活动段有:5 个客户 ID 已查看 3 addtocart 有 2 已成交 1
想要的是:
对于 customerid 1:此用户属于上述所有细分市场,我想在数据框中再创建一列,并将排名 1(因为它的 customerids 数量最少)给 customerid 的交易细分市场,对于其他 customerids 类似,我们得到如下的数据框
| customerid | segment | Segment Priority |
|---|---|---|
| 1 | active | 4 |
| 1 | viewed | 3 |
| 1 | addtocart | 2 |
| 1 | transacted | 1 |
| 2 | active | 3 |
| 2 | viewed | 2 |
| 2 | addtocart | 1 |
| 3 | active | 2 |
| 3 | viewed | 1 |
| 4 | active | 1 |
| 5 | active | 1 |
【问题讨论】:
标签: pandas dataframe data-manipulation