1

I have a dataframe df that is indexed by customer id. and includes: df=['Customer ID', 'Sales' ,'Product code' ,'Price']]: https://i.stack.imgur.com/vP8Gy.png

I want to create a column Quantile, which which calculates for each customer id the corresponding quantiles from the range (0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9,0.95,1) of the price column

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

so the final df will include a new column called quantile of the price for each corresponding customer 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

Anyone can advise what function i can use to get this?

Thank you in advance.

  • https://stackoverflow.com/questions/38356156/dataframe-add-column-whose-values-are-the-quantile-number-rank-of-an-existing-c – luca Aug 12 '23 at 16:00

1 Answers1

0

To create 12 approximately equal segments (called duo-deciles or dodeciles) of customers you should apply qcut() function to the "price" column and assign labels from 1 to 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])
BlackMath
  • 1,708
  • 1
  • 11
  • 14
  • Thank you @BlackMatch. But is there any way the labels to be the actual value of the corresponding quantiles? Or for that, i would need to calculate the quantiles of the price separately to find out the actual values? – user13425814 Oct 05 '21 at 14:51