0

我有一个由客户 ID 索引的数据框 df。并包括: df=['Customer ID', 'Sales' ,'Product code' ,'Price']]:https ://i.stack.imgur.com/vP8Gy.png

我想创建一个分位数列,它为每个客户 id 计算(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9,0.95,1) 价格列范围内的相应分位数

df=['Customer ID', 'Sales','Product code', 'Price', 'Quantiles Price']

Customer ID Sales   Product code    Price
1218            13          46      2
1219            14          47      3
1220            15          48      4
1221            16          49      5
1222            17          50      6
1223            18          51      7
1224            19          52      8
1225            20          53      9
1226            21          54      10
1227            22          55      11
1228            23          56      12
1229            24          57      13

所以最终的 df 将包含一个新列,称为每个相应客户 ID 的价格分位数:

Customer ID Sales   Product code    Price   Price Quantiles
1218            13          46      2           7
1219            14          47      3           2
1220            15          48      4           3
1221            16          49      5           2
1222            17          50      6           2
1223            18          51      7           4
1224            19          52      8           7
1225            20          53      9           7
1226            21          54      10          11
1227            22          55      11          11
1228            23          56      12          11
1229            24          57      13          11

任何人都可以建议我可以使用什么功能来获得这个?

先感谢您。

4

1 回答 1

0

要创建 12 个大致相等的客户细分(称为 duo-deciles 或 dodeciles),您应该将qcut()函数应用于“ price”列并分配从 1 到 12 的标签。

import pandas as pd
df['Quantiles Price'] = pd.qcut(df['price'], q=12, labels=[12,11,10,9,8,7,6,5,4,3,2,1])
于 2021-10-05T14:46:07.727 回答