1

I have a dataframe df that is indexed by customer id. and includes: df=['Customer ID', 'Sales' ,'Product code' ,'Price']]: https://i.sstatic.net/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.

1

1 Answer 1

1

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])
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.